public class SuperClass {
private final Object finalizerGuardian = new Object(){
@Override
protected void finalize(){
System.out.println("SuperClass finalize by the finalizerGuardian");
}
};
@Override
protected void finalize(){
System.out.println("SuperClass finalize by the finalize method");
}
public static void main(String[] args) throws Exception{
SubClass sub = new SubClass();
sub = null;
System.gc();
Thread.sleep(500);
}
public class SubClass extends SuperClass{
@Override
public void finalize(){
System.out.println("SubClass finalize by the finalize method");
}
它是一個終結者守護者在anoymous類,爲什麼這個終結者守護者總是被稱爲超類運行?thx時。關於終結者守護進程
我知道它在調用這個main方法時創建了一個finalizerGuardian的實例,但是它並沒有創建超類對象,因爲它只是創建了一個子類對象,所以爲什麼這個finalizerGuardian被調用了? – Carlos
啓動一個調試器,你會看到finalizerGuardian將在創建SubClass時實例化。這是繼承的特點之一。 – Frank