Rahul mir wrote:This question is from K&B SCJP book page no. 269.
When // doStuff is reached, how many objects are eligible for GC?
from my point of view there is only 1 object is eligible for GC, c1. But the explanation given in Book i am not getting.
Books explanation : Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible..
Please someone explain me ...
When we call new CardBoard() 2 objects are created. 1st the new CardBoard object and 2nd the instance variable in it. i.e Short.
CardBoard c3 = c1.go(c2); at this line c3 is assigned with null, but there is no change to c2.
c1 = null; at this line we lose reference to the object created at CardBoard c1 = new CardBoard(); so this will be garbage collected along with it's instance variables.