This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "garbage collection" Watch "garbage collection" New topic
Author

garbage collection

David G Harris
Greenhorn

Joined: Sep 23, 2009
Posts: 6


Hi guys, a Self Test question on page 269 of Sun Certified Programmer for Java 6 says

class CardBoard {

Short story = 200;
CardBoard go (CardBoard cb) {
cb = null;
return cb;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard c3 = c1.go(c2);
c1 = null;
if (c3 == null)
System.out.println("c3 is null");

}
}

The question is, "How many objects are eligible for garbage collection and the answer given, is 2 - c1 and c1.story. How come c3 isn't eligible?
Adolfo Eloy
Ranch Hand

Joined: Mar 21, 2009
Posts: 146

Hi David.

The c3 variable is referring to null.
It was never referring to any object.
So you do not need to worry about c3 variable.

The point is:
reading the question at K&B. when // doStuff is reached, only the object that was referenced by c1 will be eligible for GC.
Regarding the instance variable story of type Short that belongs to CardBoard we need to consider 2 instances eligible.


Adolfo Eloy
Software Developer
OCPJP 6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: garbage collection
 
Similar Threads
Garbage collection
Question from mock test
Objects - GC
help scjp5.0 question
Problem in question from K&B