Consider the following program and predict the output:
class Base {/* w ww .j a v a2s . c o m*/ protected void finalize() { System.out.println("in Base.finalize"); } } class Derived extends Base { protected void finalize() { System.out.println("in Derived.finalize"); } } public class Main { public static void main(String []args) { Derived d = new Derived(); d = null; Runtime.runFinalizersOnExit(true); } }
CannotRunFinalizersOnExitException
.b)
The statement Runtime.runFinalizersOnExit(true);
makes sure that the finalize method is invoked when the application exits.
Here, the dynamic type of the object is "Derived," so the finalize()
method of the Derived class is invoked before the application exits.