You are on page 1of 19

Constructor

Constructor
Constructor is a special method in java class. It is used to initialize a new object variables. A constructor has the same name as the class. For example if you have Color class the constructor of color class is also Color( );

Constructors cannot return any value not even void. If we are not providing any constructor for a class than default (no parameter) constructor is automatically provided by the runtime system. When we will create our own parameterize constructor. We have to create our own default constructor as well because then default constructor is not available. The constructor can be private, public or protected.

Java supports name overloading of constructors so that a class can have number of constructors with the same name. The compiler will determine at run time which constructor to call by matching the number and type of arguments you are passing into the constructor.

this
Java this keyword is used to invoke any method or variable of current class. The syntax of method is: this.methodname( ); The syntax of variable is: this.variablename = 7;

If you want to invoke another constructor of current class inside constructor you can use this statement as follows: this(4, 5); The above statement will invoke the current class constructor that takes two int arguments. Call to current class constructor with this would be the first statement in the constructor.

Destroying Objects
(Garbage Collection)

Object Destruction & Garbage Collection Some programming languages required that you keep track of all the objects you create and then you explicitly destroy them when they are no longer needed.

The Java platform allows you to create as many objects as you want and you don't have to worry about destroying them.

The Java runtime environment deletes objects when it determines that they are no longer being used. This process is called garbage collection.

An object is eligible for garbage collection when there are no more references to that object for example if object goes out of scope. You can explicitly drop an object reference by setting the variable to the special value null.

If you want to invoke garbage collector in the program you can do this by issuing following commands.

System.gc(); Runtime.getRuntime().gc();

finalize() method If you want an object to clean up its state before it is deleted from the memory, you can declare finalize() method in the class. This method will be called by the garbage collector before deleting any object of this class.

This method is inherited from the parent Object class in all java classes and can be overridden like the following code:

protected void finalize() { super.finalize(); // clean up code here. }

Class Variables
(static variables)

Class Variables
The runtime system allocates class variables once per class regardless of the number of instances created of that class. The system allocates memory for class variables the first time it encounters the class at class load time. All instances share the same copy of the class variables. You can access class variables through an instance or through the class itself.

To declare a class variable follow the following syntax. static Type variable; e.g. static int a; static double d; If any object change the value of class variable, the change will effect the values of all the variables.

Because the class variable belongs to the class rather than any specific variable, it can be accessed using the class name such as: ClassName.variableName; Suppose you have class called Car and class variable called gears than you can access that variable like this: Car.gears;

Class Variable
vs.

Instance Variable

Class vs. Instance Variables


Class variable Instance variable

Class variable is declared by Instance variable is declared using static keyword. without static keyword. ;int a = 4 ;static int a = 4 All objects share the single All objects have their own copy of static variable. copy of instance variable.

Static variable does not Instance variable depends depend on the single object on the object for which it is because it belongs to a class. available.

Static variable can be Instance variable cannot be accessed without creating an accessed without creating an object, by using class name. object. ClassName.variable ObjectName.variable

You might also like