I've the following piece of code.
class SuperClass
{
public void finalize() throws Throwable
{
System.out.println("finalize() in SuperClass...");
}
}
public class FinalizableObject extends SuperClass
{
public static void main(String[] args) throws Exception
{
FinalizableObject obj = new FinalizableObject();
Thread.sleep(1000);
obj = null;
System.gc();
}
public void finalize() throws Throwable
{
super.finalize();
System.out.println("finalize() in FinalizableObject...");
}
}
I get all 4 possible outputs
1. finalize not called for sub/super class.
2. finalize called for only sub class.
3. finalize called for only super class.
4. finalize called for both super and sub class.
... in different runs of the program. Why is it so?
My question is not that GC 'should' run on System.gc() invocation. When a sub class finalize() has been called, why doesn't the superclass finalize gets called, even though the call is explicit.