Java Object finalize() Method
The Object finalize() method is used for operations triggered when an instance is recycled by the garbage collector.
When the GC (Garbage Collector) determines that there are no more references to the object, the object's garbage collector will call this method.Syntax
protected void finalize()
Parameters
None 。
Return Value
No return value.
Example
The following example demonstrates the use of the finalize() method:
Example
import java.util.*;
class ExampleTest extends GregorianCalendar {
public static void main(String[] args) {
try {
//Create a ExampleTest object
ExampleTest cal = new ExampleTest();
//Output the current time
System.out.println("" + cal.getTime());
// finalize cal
System.out.println("Finalizing...");
cal.finalize();
System.out.println("Finalized.");
} catch (Throwable ex) {
ex.printStackTrace();
}
}
}
The above program execution result is:
Sun Oct 11 11:36:46 CST 2020 Finalizing... Finalized.
Other extensions
Java Object Class