当对象引用变量被声明为final [duplicate]时,它何时才有资格进行垃圾回收

时间:2021-05-07 03:49:48

This question already has an answer here:

这个问题在这里已有答案:

Here is my code snippet where i am using a final object reference..

这是我的代码片段,我在使用最终的对象引用。

public class FinalTest{
    private int rollNo;
    public static void main(String[] args){
        final FinalTest obj = new FinalTest();
        obj.rollNo=20;

        obj.rollNo=30;

        System.out.println(obj.rollNo);
        obj = null; 

    }
}

and finally i am assigning null to the reference variable obj.. but java does not allow this. so i want to know in such case ( when we don't assign null to our object reference obj) when does this obj will become eligible for garbage collection.

最后我将null赋给引用变量obj ..但java不允许这样做。所以我想知道在这种情况下(当我们没有为我们的对象引用obj赋值null)什么时候这个obj将有资格进行垃圾收集。

1 个解决方案

#1


3  

The object, which obj holds reference to, should become eligible for garbage collection when the method completes, because the program flow should be already on a different execution scope (i.e. other method/class)

当方法完成时,obj持有引用的对象应该有资格进行垃圾收集,因为程序流应该已经在不同的执行范围(即其他方法/类)

#1


3  

The object, which obj holds reference to, should become eligible for garbage collection when the method completes, because the program flow should be already on a different execution scope (i.e. other method/class)

当方法完成时,obj持有引用的对象应该有资格进行垃圾收集,因为程序流应该已经在不同的执行范围(即其他方法/类)