Which of the three objects, A, B, C, are eligible for garbage collection when method m2 begins to execute? a. A b. B c. C d. None. e. Compiler error. f. Run time error. g. None of the above. Answer is a and b. Can anyone explain how? Thanks
Sunita, This question has been discussed a number of times in this forum (once just recently, as well, I believe). Take a look at this thread. Be sure you do a search through the forum. Sometimes, you'll find what you're after right away. Corey
i1,i2 and i3 are member variables of J. So when we call J() we have
Then we call private instance method m1(). We have i1.other(i2); I would think that in method other, a new variable i is created which again points to new I(a)??? What happens when inside other we say other=i??
[ March 26, 2003: Message edited by: Corey McGlone ]
Joined: Dec 20, 2001
Every I object has two members, a reference to a String which represents its name and a reference to another I object, named other. When we initialize J, we create 3 new objects, like this:
In this case, the letter represents the name, the null represents the value of the member variable other for each I object. Now, when we invoke the method m1, we start invoking the other method of our I objects. Executing this line of code:
causes the method I.other to be invoked. So, if we follow the execution to that method, we see this code:
This line simply assigns the value of i, which, in this case, is a reference to the object referenced by i2 (that's the I object named B), to the member variable other. So, now our picture has changed slightly - it now looks like this:
As you can see from the picture, the object named A now contains a reference to the object named B. The next two invocations of the method m1 perform a similar function, just on different objects. Once they're done, our picture looks like this:
The objects named A and B have references to each other and object C contains a reference to itself. Now that we're done invoking the m1 method, we can go on to the next lines of code, that do this:
These lines simply reassign i1 and i2 to reference the same object that i3 references. Again, our picture changes to look like this:
Now, you can see that i1, i2, and i3 all reference the same object and the only things that reference objects A and B are each other. As those objects can no longer be accessed via an active part of the application, they can be safely garbage collected. Object C, on the other hand, can still be accessed via the member variables i1, i2, or i3, so that object is not eligible for garbage collection. I hope that helps, Corey
Joined: Oct 03, 2002
Thanks Corey for taking the pain to explain it clearly.
Joined: Dec 20, 2001
Originally posted by sun par: Thanks Corey for taking the pain to explain it clearly.
It's really no problem. Quite often, people that have been around a while simply redirect you to other sources (such as was the case here) because we know that it's already been discussed and there is already a good deal of information there. It's not that we're trying to blow you off. If you take a look at what I show you and still have questions, I'd be happy to help you out - I'm sure almost everyone around here would do the same. Corey