FAQ

Java

JSP

Servlet


Advertisement



Can finalize() be called more than once on an object?

The finalize() method is never called more than once by the JVM for any given object, but it can be also programatically called any number of times.

class MyObject { 
public void finalize () throws Throwable {
super.finalize();
System.out.println("finalize");
}
}

class Program {
public static void main(String[] args) throws Throwable {
MyObject obj = new MyObject();
obj.finalize(); //Line A
obj.finalize(); //Line B
System.out.println("Prepare GC...");
obj = null;
System.gc();

}
}

The output is

finalize
finalize
Prepare GC ...
finalize


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |