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
Comments
Post a Comment