lass T{
boolean empty=false;
T(boolean status){empty=status;}
void dropout(){empty=true;}
protected void finalize(){
if (empty==false)
System.out.println("error:the tank is still not empty!");
}
}
public class Tank {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
T tank1=new T(false);
T tank2=new T(false);
tank1.dropout();
System.runFinalization();
System.gc();
}}为什么没有输出error:the tank is still not empty这句话啊,有些困惑~~求帮助!
你试试这样:
T tank1=new T(false);
T tank2=new T(false);
tank2 = null;
System.gc();
最后最好在放个for空循环 别让程序还没执行垃圾回收就结束