^
Jan tenpas wrote:When line 16 is reached, how many objects will be eligible for garbage collection?
Answer: 1
hello jan, i had the same question some monthes ago. the rule you must apply here is that an object is only eligible by the gc when it has no references on it. perhaps to understand this gc problem it helps you to differentiate between stack and heap. the stack is a piece of memory where all references to objects are stored. the heap is a piece of memory where the objects live.
in the given code four objects are instantiated, lets call them A1, A2, B1 and B2. the object B1 is referenced by b1, A1.b1 and A1.b2. so B1 will not be eligible by nulling b1, because the other references are still there. B2 has two references on it, on by b2 and the other by A2.b2. so even by nulling the b2 there is still one reference on it, so this object will not be eligible. the next object is A1 that has only one reference from a1. so by nulling a1 this object will be eligible by the gc. the last object is A2 that has also only one reference from a2. but this reference is not set to null so also this object is not eligible for the gc. so looking once again at the four objects we can see that only 1 object is eligible. so the answer given in the book is right
if you need further help, let me know.