Will an element be garbage collected if there is a reference to its field?

后端 未结 4 1416
粉色の甜心
粉色の甜心 2021-01-17 12:31

I.e., in

class A {
    public String s;
}

and

A a1 = new A();
a1.s = \"bla\";
A a2 = new A();
a2.s = a1.s;
a1 = null;
         


        
4条回答
  •  借酒劲吻你
    2021-01-17 13:13

    Here you are creating two references of Object A like a1 and a2.

    First, you are assigning value of a1 to a2.So after setting value to a2, a1 is allowed for GC. But there will be no change in reference a2.

    You can also check this blog for Garbage Collection:

提交回复
热议问题