Why execution of finalize()
isn't guaranteed at all in Java? Is finalize()
method shouldn't be used???
Consider below program.
class Test{
protected void finalize()
{
System.out.println("Will i execute?");
}
public static void main(String args[])
{
Test t=new Test();
}
}
There is an empty output when this program runs. We know that finalize()
is used to cleanup any external resources before the object becomes eligible for garbage collection & finalize()
will be called by JVM. Inside finalize()
we will specify those actions that must be performed before an object is destroyed. Is finalize()
method evil??