class Example {
@Override
protected void finalize() {
System.out.println("Object getting garbage collected");
}
}
public class GarbageCollectionDemo {
public static void main(String [] args) {
Example e1 = new Example();
{
Example e2 = new Example();
}
e1.finalize();
}
}
I wanted to check whether the output shows when the finalize()
is called, and when the objects are actually getting removed. But I could not track, is it because its according to the JVM that the object will be garbage collected? What happens if we override the finalize()
method? Do we have to explicitly call it ourselves? Then why does Netbeans suggest to add the finally
block with super.finally()
?