The following code is from: John Meyers's
SCJP 5 Mock Exam (Inquisition)
My answer was 1.
But the answer given is 2 and the explanation given is:
The class test has a variable tester that points to another test object. The reference to both objects are lost when variable t is nulled. Thus two objects are eligible for garbage collection. My explanation for my answer is:
t = new test(); --> New object pointed by t
t = new test(t); --> Invokes the arg'ed constructor and
tester now points to the same object that t is pointing.
Coming back to the call, the reference t is now pointing to the new object thus created.
t = null; --> Make t point to null which leaves us with just one object eligible for GC, which is the object created by this line:
t = new test(t); Am I missing something?