One thing you might try to do as a learning exercise is to map it out, see what it looks like, and make a determination.
Each time one is created, draw a reference variable pointing to a circle or something representing the object. If it has a reference inside that got set via a constructor, go ahead and make it point to the object that it is set to. Later, when you get to where the class member references are set to point to each other, just draw the arrows. When a reference is nulled just erase or scribble it out. Then, see what is left and think like a "thread". "Do I have any way that I can get to that object? No? Then it is eligible for GC.
DO a couple of these and it will all make sense.
------------------------
Bob
SCJP - 86% - June 11, 2009
I think that you are correct. As both i3 and i4 are made null. So, there is no reachable reference to the objects created for i3 and i4. Though i3 internally refers to i4, but as i3 cannot be reached, i4 also can't be. i2 reference is still referring to the object created for i2. So, only i3 and i4 are eligible for GC.
When it's obvious that you have to do it, just do it without shattering your thoughts over different directions.
Actually i3.i is pointing to i4 object, then how i4 object will be eligible for gc? if any object is having a single reference then it cant be eligible for gc.
When looking for GC eligibility what you should look for is a reference that any runnable thread can use to reach a specific object. Your observation that i3.i points to i4 is true. But there is no reference pointing to i3 that a thread can use to reach i3 so, if a thread can not reach i3 then it follows that a thread can not reach i4.
------------------------
Bob
SCJP - 86% - June 11, 2009
@Preetha There are two pssible ways to reach the object created for i4 - 1. through the i4 reference variable. but it is made null. 2. through i3 as i3 is referring to i4 internally. But i3 itself is made null, that means by i3 there is not way to reach i4. So, there is basically no other way to reach the object created for i4 from the outside world of heap. That means no live thread can reach that object now. Thats why the object created for i4 is also eligible for GC.
When it's obvious that you have to do it, just do it without shattering your thoughts over different directions.
I Agree with all, There is no way a runnable thread can reach i3 and hence i4 that makes both of them eligible for GC. i2 is the future KING [ December 11, 2008: Message edited by: Himalay Majumdar ]
But remember that you can not force the jvm to gc. You can only ask it.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
In the renaissance, how big were the dinosaurs? Did you have tiny ads?
Free, earth friendly heat - from the CodeRanch trailboss