The Java Object finalize() method is called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.
DeclarationFollowing is the declaration for java.lang.Object.finalize() method
protected void finalize()Parameters
NA
Return ValueThis method does not return a value.
ExceptionThrowable − the Exception raised by this method
ExampleThe following example shows the usage of java.lang.Object.finalize() method. In this program, we've created a class ObjectDemo by extending GregorianCalendar class. In main method, finalize is called and object is made available for garbage collection.
package com.tutorialspoint; import java.util.GregorianCalendar; public class ObjectDemo extends GregorianCalendar { public static void main(String[] args) { try { // create a new ObjectDemo object ObjectDemo cal = new ObjectDemo(); // print current time System.out.println("" + cal.getTime()); // finalize cal System.out.println("Finalizing..."); cal.finalize(); System.out.println("Finalized."); } catch (Throwable ex) { ex.printStackTrace(); } } }Output
Let us compile and run the above program, this will produce the following result −
Sat Sep 22 00:27:21 EEST 2012 Finalizing... Finalized.
java_lang_object.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4