The Java object class provides a finalize () method with the following signature:
protected void throws Throwable {}
This method is used for garbage collection by the JVM, so any class can override the method to allow an instance of its own class to perform a series of actions before being recycled.
There are two ways to enable the JVM to be actively garbage collected:
System.GC (); Runtime.getruntime (). GC ();
However, the JVM does not guarantee that GC operations are performed immediately.
The Finalize () method uses the following example:
Public classTest { Public Static voidMain (string[] args) {//use of FinalizeA AA =NewA (); Try{aa.finalize (); } Catch(Throwable e) {e.printstacktrace (); } AA=NULL; System.GC (); }}classa{@Overrideprotected voidFinalize ()throwsthrowable {System.out.println ("Finalize!!!!"); }}//Output://Finalize!!!!//Finalize!!!!
The Finalize () method of the Java class