Java Object finalize() Method
Finalize() is the method of Object class.
This method is called just before an object is garbage collected. finalize()
method overrides to dispose system resources, perform clean-up activities and
minimize memory leaks.
Fig: Finalize In Java |
Syntax:
protected void finalize() throws Throwable
Throw
Throwable - the Exception is
raised by this method
Example
public class JavafinalizeExample1 {
public static void main(String[] args)
{
JavafinalizeExample1 obj = new JavafinalizeExample1();
System.out.println(obj.hashCode());
obj = null;
// calling garbage collector
System.gc();
System.out.println("end of garbage collection");
}
@Override
protected void finalize()
{
System.out.println("finalize method called");
}
}
Output:
2018699554
End
of garbage collection
finalize
method called
No comments:
Post a Comment