Thursday 12 October 2017

Finalize() Method in Java

The Finalize() Method


Java.lang.object.finalize is called by the garbage collector or an object.

Before an object is garbage collected the run-time system calls finalize() method.

The aim is for finalize() is to release system resources such as open file before getting collected.

Finalize() method work after the execution and before garbage collection.

Finalize() method does not return any value to the return type is void.Protected keyword is access-specifier so that any object of outside class cannot access to this method.

The Finalize() is called just before when java run-time system performs garbage collections.

→"Sometimes you will need to do some operations when an object is destroyed by garbage collection. For example, when your program is using some non-java resources, you have to free the memory for these resources. This process is known as finalization. It is done by finalize() method.

Syntax :
  
                  Protected void finalize()
                  {
                                //Statements;

                   }

Popular posts