未被引用的对象能否被再次引用?

Can an unrefrenced objects be refrenced again?

正如主题所说:未引用的对象可以再次引用吗?

我在 http://www.javatpoint.com/corejava-interview-questions-4 Q120 遇到了这个问题。我为此尝试使用谷歌搜索,但没有找到任何 link。我们实际上是怎么做到的?

下面的示例不是 "in the wild" 示例,但它演示了如何通过 finalize 方法 "resurrected" 取消引用对象。这可能只会发生一次。如果第一个实例的对象第二次变为未引用,则不会再次调用 finalize() 方法。

public class Resurrect {

    static Resurrect resurrect = null;

    public static void main(String[] args) {
        Resurrect localInstance = new Resurrect();
        System.out.println("first instance: " + localInstance.hashCode());

        // after this code there is no more reference to the first instance
        localInstance = new Resurrect();
        System.out.println("second instance: " + localInstance.hashCode());

        // will (in this simple example) request the execution of the finalize() method of the first instance
        System.gc(); 
    }

    @Override
    public void finalize() {
        resurrect = this;
        System.out.println("resurrected: " + resurrect.hashCode());
    }
}