Day26 : Object Class and Initialize Method

 Object Class and Initialize Method

In Java, there is a special class called object available in the package java.lang. Specially of this class in that every java class which does not inherit of any other class automatic inherit of any other class.
In Other word we can say every class in java directly or indirectly becomes child class of object class.

The class object has a bunch of useful method predefined into it by java. And since every class inherit the class object so these method are inherit by every other class in java.
Amongst these method, there is a special method called finalize which has been following body in object class.
protected void finalize()
{

}

The finalize method is automatically called by JVM during object destruction i.e., when the garbage collector remove an object from heap area, then for every object it calls finalize method.
So, if we want to execute some piece of code during object destruction then we should overwrite finalize method in our class as shown below:
protected void finalize()
{
---nextId;
}
But if we get finalize to execute on object instruction then we have to take three extra steps:
  1. Explicitly set the references to null.
  2. Call the static method called garbage collector belonging to system class.
  3. Call another static method called run finalization belonging to system class.
        x = y =null
        System.gc()
        System.runFinalization()

One last point to remember about finalize is that it has been deprecated by java from java 9 onwards And thus union it extremely important we should not overwrite use this method.

Comments

Popular posts from this blog

Day2 : Compiling Java Program with Examples

Day1: Introduction to Java and its feature

What does System.out.printf( "%-15s%03d\n", s1, x) do?