首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

这么写会不会导致对象无法被回收

2013-01-20 
这样写会不会导致对象无法被回收这样写会不会导致Test2的实例无法被回收?package test0public interface

这样写会不会导致对象无法被回收
这样写会不会导致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()方法,里面输出点东西,发现可以回收的。(当然需要产生很多对象)。

热点排行