This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Well, the Cardboard object holds on to a Short instance, right? So when the Cardboard instance is eligible, so is the Short instance, because there are no other variables that hold a reference to the Short object.
The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
An object is eligible for garbage collection when there are no more references to that object. The way I would solve these questions is to dry run. Just by looking at the code c3 & c1 will have NULL. Please check out Oracle tutorial.
Please SearchFirst. This question has been asked many times in the past. In fact, a quick search yield about three pages of hits. The first three hits on the first page (not counting this topic) were...
Post Today 9:57:27 AM Subject: Garbage Collection
But what is the reason for that c3 is not eligible for GC?
I believe you are asking why c3 is not eligible for garbage.
Reason: Because c3 has no reference to existing object and also have not created any instance of new object. So there nothing present for Garbage collection.
Terminology is EXTREMELY IMPORTANT. Strictly speaking, c3 is a reference variable. Reference variables are NEVER eligible for garbage collection. As a reference variable, c3 might be referring to an object. At some point, when that object has no "live" reference variables pointing to it, that object can become eligible, but reference variables are never eligible.
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Here two objects are eligible for Garbage collection. First of all we are explicitly assigning null to the C1. That's why c1 is eligible for Garbage Collection. Then C3 is assigned by calling method on C1. As c1 is null, c3 is also eligible for garbage collection. So totally 2 objects are eligible for garbage collection.
Joined: Oct 14, 2002
you MUST come up with a concrete way to refer to objects. given that references to objects can be re-assigned, you have to say things like:
"the object referred to by var c1 at line x"
it's NOT sufficiently precise to say "c1 is eligible for GC".
c1 is a reference var, it's NOT an object. as the code runs, c1 might start off by referring to one object, and then later on it might be changed to refer to a different object.