日期:2014-05-20  浏览次数:20635 次

这样写会不会导致对象无法被回收
这样写会不会导致Test2的实例无法被回收?


package test0;

public interface ITest2 {
void showMessage();
}




package test0;

public class Test2 implements ITest2 {
private static final ITest2 instance = new Test2Impl1(); 

@Override
public void showMessage() {
instance.showMessage();
}

}




package test0;

class Test2Impl1 implements ITest2{

@Override
public void showMessage() {
System.out.println("hello world.");
}

}


------解决方案--------------------
没有强引用就可能被回收
------解决方案--------------------
不会吧,应该是可以回收的。 主要还是看你的容器,怎么个回收机制。
------解决方案--------------------
让Test2重写了finalize()方法,里面输出点东西,发现可以回收的。(当然需要产生很多对象)。