Regards
KumarRaja
Ben Synes wrote: Ive seen this same question time again on many past posts in this forum.
Ben Synes wrote:
Kumar Raja wrote:My guess is 4 objects qualify for GC
a1,a1.b2,b1, b2
a1.b1 is not marked for GC, as it is shared by a2 .
Ben Synes wrote:
My question is not how many objects are candidates for garbage collection, I know the answer the answer is 1, a1. But my QUESTION is, how does a2s Beta b1 get set to be b1?? Please read my post again carefully.
you will see that four objects have been created -- using the "new" operator. So... hint...
Kumar Raja wrote:My guess is 4 objects qualify for GC
a1,a1.b2,b1, b2
a1.b1 is not marked for GC, as it is shared by a2 .
So, you are saying that all four objects, that have been created, are eligible for GC?
Regards
KumarRaja
Kumar Raja wrote:
I think, I realized my mistake.
Given the a1 = null, b1 = null, b2 = null
but since b1 -> a1.b1 and b1 ->a1.b2 i.e a1.b1 and a1.b2 point to the same object as b1 is pointing and since a1.b1 being a static and at class level it is shared with a2.b1
even though b1 = null, a1 = null, but since a2 != null, a2.b1 still holds the pointer to the same object as a1.b1. So this makes b1 still alive. and so the case with a1.b2.
So the only object that is GC is a1.
Regards
KumarRaja
Henry Wong wrote:
Hint: Is there a difference between "a1.b1" and "a2.b1"? Or do they both get converted to "Alpha.b1"?
Ben Synes wrote:
Yep I got it, now it all seems to clear. But this would not be the case if Beta b1 = new Beta() had not been called. Before that point, the class variable b1 in Alpha would be a reference to null I presume?