I want to check the condition in termination with finalize, but finalize never be executed each time. Can anybody tell me why?
public class Test
{
public static void main(String[] args)
{
Tank tank=new Tank();
tank.fill();
System.gc();
}
}
public class Tank {
private boolean emptied=true;
public void fill()
{
this.emptied=false;
}
public void empty()
{
this.emptied=true;
}
public Tank()
{
this.emptied=true;
}
protected void finalize()
{
if(this.emptied==false)
{
System.out.println("Termination Verification Error: Tank should be emptied");
}
}
}