Home  |   STIU  |   WOW  |   SCJP  |   SCDJWS   |   JEE FAQ  |   About US  |  

FAQ
  Java FAQ
  JSP FAQ
  Servlet FAQ
 

Advertisement

XyzWs Java FAQ:
Can finalize() be called more than once on an object?


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
Advertisement
XyzWs Java FAQ: Can finalize() be called more than once on an object?

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

 


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

Support  | Feedback  | Help