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:
- A constructor does not have any return type (even not void).
- the name of the constructor must be same as the name of the class in which it is defined.
- 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:
- Default constructor.
- 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 :
- When null is used after utilization of object.
- When we overlap any Object reference.
Demo d2=new demo();
- 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.