In case of the object referenced by b2, it is not eligible for GC, because it has an active reference a2.b2. when a1=null, so 1 object for GC. So when a1=null, will the references to b1,b2 be lost?? i.e. The objects referenced by b1,b2 will also be eligible for GC? In that case, b1 will also be elegible for GC.
Who said there are not objects.... They are objects.(Created by using new operator). If your question is why they are not deleted its because they are still refered. b1 is refered by a2.b1 b2 is refered by a2.b2
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
I am Vaishali.
I am following this example.
My doubt is when we say only a1's object is eligible for GC, do we mean Alpha object is gced or a1.b2 is gced?
If we say a1 object as whole is Gced then why the count[Total no of objects eligible for GC] is not 2 as one is Alpha object that has one Beta object as its Instance.
I see two examples from K&B book Q1 and Q10 Chapter 3.
Q1. Here one Cardboard object is eligible for GC which has Short Wrapper Object so count=2.
Q10. Here one Dozen object is eligible for GC which has an array so count=2.
Someone please help. I am badly stuck here.
Sorry if I still need to understand some concepts right.
Thanks a lot.
I realize it's several months later, but did anybody ever answer the question from Vaishali? I am confused about the exact same thing.
I understand that a1 is assigned null, so the object it was attached to is eligible for GC. What confuses me is, what about a1.b2? Isn't that a separate object that would be eligible for GC also, making two objects GC? Apparently a1.b2 is not counted as a second object eligible for GC. Is it because it is still attached to b1? Is it because it is a reference variable, not actually an object? (I'm new at this, and I'm having a hard time getting my brain around the difference between reference variables and objects.)
If anybody could help, I would greatly appreciate it.
okay....i think there is a bit of confusion between objects and references here . a1.b2 is not an object and is a reference. you assign it to an object .
a1.b2 = b1 ;//It means that a1.b2 points to an object that is already referenced by b1 . a1.b2 by itself IS NOT AN OBJECT
so when you make a1 = null , it means that you are just removing of the reference and since the object earlier referenced by b1 can still be accessed by a2.b1 it will not be eligible for garbage collection.
NOTE : if they had inserted a code a1.b2 = new Beta () ; instead of "a1.b2 =b1" Then there would have been two objects for GC .
Sun thought (we'll see if Oracle does too) that GC was an important concept, so it's on the exam.
The important concepts are:
- what's an object (and only objects get GCed)
- what's a reference to an object
- a single object can have multiple references
If you have those concepts really clear in your mind, you should be able to draw pictures for these kinds of questions to determine the objects, their various references, and therefore what's eligible for the GC.
Of course in the real world you might be fixing someone else's code, and they might not use something like the 'static' nomenclature mentioned here, so you need to be able to spot references as they appear "in the wild"
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)