1.

Purpose of finalize() in Java

Answer»

The finalize() is a method which is used to clean up the processing before the garbage COLLECTION in java. It is called by the garbage collector of an object when there are no further references to that object.

finalize() is overridden by a child class to get rid of the system resources or to conduct other cleanup. The exception raised by this method is called the THROWABLE exception.

The java.lang.Object.finalize() method does not take any parameters and does not return a value.

Now let us look at the overriding of finalize() method in Java,

public class Example {    public static void main(String[] args)    {        String STR = new String("Example");        str = NULL;        System.gc(); // prompts the JVM to call the Garbage Collector        System.out.println("End of main method");    }    public void finalize()    {        System.out.println("This is the finalize method");    } }

The output is as follows

$JAVAC Example.java $java Example End of main method


Discussion

No Comment Found