| Java FAQ | ||
| JSP FAQ | ||
| Servlet FAQ | ||
XyzWs Java FAQ:
Can finalize() be called more than once on an object?
Printer-friendly version |
Mail this to a friend
|
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
|