Sunday 8 January 2012

Java Concepts: Construction and Destroying Object

Constructor- Constructor is a operator which is used for creating an object. It is quite similar to the object but have some restriction such as:
  1. A constructor does not have any return type (even not void).
  2. the name of the constructor must be same as the name of the class in which it is defined.
  3. The only modifier that can be used on a constructor definition are the access modifier such as Public, Protected and private.(constructor are never static).
  • Constructor is used for initialization of such objects which have same initial value and we want to put these values at the time of object creation.such as in the industry when we make a entry of fresher than if we want to enter the initial salary at the time of creation than we use it by using constructor.
  • There are two kind of constructor:
  1. Default constructor.
  2. Explicit constructor.



Garbage Collection- Garbage collection is the process of  freeing waste memory for reusing purpose.The memory which is allocated in heap is collected because size of heap is very limited. Only those objects which does not have reference variables are available for garbage collection.There are various situations for the garbage collection :
  1. When null is used after utilization of object.
  2. When we overlap any Object reference.
such as Demo d1=new Demo();
            Demo d2=new demo();
                      d2=d1;

  • We use System.gc() method for requesting JVM for garbage collection.
  • We use finalize() method for giving last instruction to the program to terminate the program.

Anshul.




No comments:

Post a Comment