Objects eligible for garbage collection

前端 未结 5 2110
我寻月下人不归
我寻月下人不归 2020-12-03 12:41

This question was taken from Kathy Sierra SCJP 1.6. How many objects are eligible for garbage collections?

According to Kathy Sierra\'s answer, it is C.

5条回答
  •  我在风中等你
    2020-12-03 13:25

    No object ever existed that c3 points to. The constructor was only called twice, two objects, one each pointed to by c1 and c2. c3 is just a reference, that has never been assigned anything but the null pointer.

    The reference c3, that currently points to null, won't go out of scope and be removed from the stack until the closing brace at the end of the main method is crossed.

    The object originally assigned to c1 is unreachable because the c1 reference was set to null, but the c2 reference has not been changed, so the object assigned to it is still reachable from this scope via the c2 reference.

提交回复
热议问题