Saturday, November 26, 2016

Define Constructors - what is Static keyword & constructor

          

             Use    Constructors methods   

  o   In java constructor is a special type of method which is used to initialize the objects.
  o   The constructor name is same as class name otherwise it will be taken as user defined method by          compiler.
  o   Constructor has no any return type.
  o   To invoke the constructor, create an object of that class
  o   There are two types of constructor in java
          Define Constructors - what is Static keyword & constructor .

1.       Default constructor.
2.       Parameterized constructor.

Default constructor
The constructor without parameter is known as default constructor,
Define Constructors - what is Static keyword & constructor .
Example:

pakage com.constructor;

public class DefaultContructor{

//this is default constructor
public DefaultContructor(){
system.out.println("Hello this default constructor");
}
public static void main(string[]args){
//creat the object it will invoke the constructor
defaultConstrutor contructor = new DefaultConstructor();
}
}

Define Constructors - what is Static keyword & constructor.

Note: in above example we create a constructor inside that we just print a line to invoke this constructor we create an object of class. Suppose if you don’t create constructor in your program then JVM create default constructor for you.

Parameterized Constructor
The constructor with parameter is known as parameterized constructor

Example:


Note:in above I create a parameterized constructor with two parameters two invoke the constructor we create an object for that class.


     1.       Static Keyword

  o   In java static is a keyword, which we can use with data members, methods, blocks and inner                classes.
  o   The static keyword is mainly used for memory management.
  o   The main advantage of static keyword is save memory.
  o   Static variables reserve memory only once at class area
  o   There is no need to create an object for static data members and methods.
  o   The variable creates as a static is knows as static variable
  o   The method creates as a static is knows as static method
  o   Static method belongs to class rather than object of class.
  o   For static methods and block you must have to create static data members otherwise it will create compile time errors
     Define Constructors - what is Static keyword & constructor

Example with Data Members

pakage com.static

public class dataMemberStatic{

string empName;
in       empSalary;
string  empOrganization = "zbaist";

public DataMemeberStatic(String name, int salary){
           empName =  "name";
           empSalary = "Salary";
}
public static void main(String[]arg){
DataMemberStatic MemeberStatic = new DataMemberStatic("smart", 8000);
DataMemberStatic MemeberStatic1 = new DataMemberStatic("ware", 10000);

MemberStatic.getEmplyeeDetails();
MemberStatic1.getEmplyeeDetails();
}
}

Note Point

in above example I create one data member static, because according to example every employee has same organization name, so we create it as a static variable rather to instancevariable because if I create it as an instance variable then it will occupy the memory every time when we create an object, due to static variable it occupies memory once in a class area now we call it at different places.

Note: in above example we don’t call static method through object, because it belongs to class rather than object of class. You may also call the static method with class name. you can also call above static method like that:
                MethodStaticDemo.cube(4); // 64.

       Static Block
Static block is also used to initialize the data members and it will be executing before main method
Define Constructors - what is Static keyword & constructor.




No comments:

Post a Comment