Total Pageviews

Friday, 24 June 2011

Constructor and Garbage collection

CONSTRUCTORS
The concept behind constructor is very much similar to a method, but it's not a method. A constructor is a block of code which runs when we instantiate an object.A constructor shares a common name with the class in which it is defined.The only way to invoke a constructor is with the help of keyword "new".You can have a doubt from my earlier post on objects, there also I have used "new" operator to initialize the object .
Box mybox = new Box();
I have not mentioned the word constructor there. Actually , a default constructor is created by the compiler, whenever we create a "class".And ,We cannot inherit a constructor.
declaration:
//constructor
class ConstDemo{
    //instace variables
    public ConstDemo(){
    //creating a constructor
    }
    System.out.print("/./././");
 }
In the above syntax , there is a constructor "ConstDemo()" , which has the same name as the class name.Now a problem may arise in your mind that the syntax is very much similar to "method" then how it is different from method. The main difference between a method and the constructor is , the method has some return type but the constructor does not have any return type, not even void.We are going to deal with two types of constructor (i) parameterized constructor and (ii) non-parameterized constructor or default constructor.
parameterized constructors are those constructors in which we can pass one or more arguments and non-parameterized or default constructor are those in which we don't have to pass any arguments.
We cannot inherit a constructor.
//example of parameterized constructor
class Rectangle{
 int length , width ;
 Rectangle( int x , int y) //parameters passed in the constructor rectangle.
 {
   length = x;
   width = y ;
 }
 int rectArea()
 {
   return (length*width);
 }
}
class RectangleArea{
 public static void main(String args[])
 {
   Rectangle rect1 = new Rectangle(20,3);
   int area = rect1.RectArea();
   System.out.print("Area1 =" + area);
  }
}

//example of a non-parameterized or a default constructor.
class Rectangle{
 Rectangle(){ //no parameters are passed
 length = 5;
 breadth = 6;
 }
int rectArea(){
 return (length*width);
}
}
class RectangleArea{
 public static void main(String args[]){
 Rectangle rect2 = new Rectangle();
 int area = rect2.rectArea();
 System.out.print("Area2 = " + area);
}
}

Garbage Collection
While creating an object we use the "new operator to allocate memory for the object at run time. Suppose, we have created extra objects and we do not require few of them.So, we have to delete those useless objects. In C++ , the objects are removed manually with the help of destructor (~) , but here in java it is done by the JVM.
The JVM calls the garbage collector on an object, whenever the object is not accessed by any active thread( in short we can say that a thread is a code which performs some task).The programmers can forcefully try to call the garbage collector by using System.gc() method. However calling this method dosen't ensure us that the garbage collector is called, it is just a mere request. Apart from the objects that are not being accessed by the active thread, an object can be eligible for the garbage collection on the following cases like.
1. If an object is set to null;
 eg: Myclass mc = new Myclass();
     mc =  null;// object is set to null.
2.If an object is initialized by another reference of an object.
 eg: String str1 = new String("BIPLOV");
     String str2 = new String("PRADHAN");
     str1 =str2; //object us initialized by another reference of an object.

finalize() method
The finalize() method is used to define some specific task that will occur when an object is just about to be called by the garbage collector.The java run time calls the finalize method whenever it is about to delete an object of that class.The garbage collectors run periodically, and checks whether there is sum object to be destroyed or not. If it finds one of them , then the Java run time calls the finalize() method on the object and then the selected oobject is destroyed.

Syntax :
  protected void finalize()
 {
  // finalization code here;
 }
Here, we are using the keyword protected  is the access specifier that prevents access to finalize() by code defined outside its class.

No comments:

Post a Comment