我也有这样的情况。线后c = null;
在示例的main
中方法,将Collar
对象是垃圾收集的诱饵吗?
<强> Example.java
是:
class Example{
public static void main(String[] args){
Collar c = new Collar();
Kit k = new Kit(c);
c = null;
//more code to keep the program running
}
}
<强> Kit.java
是:
class Kit{
Collar kit_col;
public Kit(Collar col){
kit_col = col;
}
}
<强> Collar.java
是:
class Collar{
public Collar(){
//nothing here
}
}
最佳答案
如果您有对 Kit
实例的引用,并且 Kit
实例有对您的 Collar
实例的引用,则两者都不能被垃圾收集。您基本上仍然可以引用两者。
关于java - 垃圾收集的对象诱饵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34093580/