You got it!
Once the method completes, all local references are gone. The key to GC issues is to NOT focus on reference counts, but instead focus on one question, "Is the object reachable from a live
thread?" Can anyone actually *get* to the object? If no, because the variables that referenced it have all gone completely out of scope at the end of a method, then the object is eligible.
If yes, because a reference to the locally-created object was then stored in an instance variable somewhere, then the object is not elible (unless the object holding the reference variable is ALSO eligible...)
If you are not sure, because a valid reference was returned (as would be the case if rg had not been first set to null) to a calling method, or passed as an argument, then... you cannot be sure.
You will need to recognize all three of those conditions:
1) Definitely eligible (still has reachable references)
2) Definitely NOT eligible (does not have reachable references)
3) Possibly, but not really sure (because you can't tell from the code whether a reference was saved or not after the method completes)
For the exam, it is not any more subtle than that. You do not need to worry about garbage collection and the
String literal pool. (You *do* need to know about String literals, but not with respect to GC).
The fun with garbage collection just never stops!
cheers,
Kathy