This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object eligable to GC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object eligable to GC" Watch "Object eligable to GC" New topic

Object eligable to GC

wei liu
Ranch Hand

Joined: Nov 19, 2008
Posts: 35

hi everyone,

Can someone help me to understand following question

in Kathy sierra and bert bates book Page 277 self test question 1

why is the CardBoard Object c3 is not eligiable to GC and why Short wrapper object is eliable?


SCJP 1.6 in 2010
SCWCD5 in 2011
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

About the Short:
c1 is no longer accessible and it contains a Short object, so that object is also eligible for GC.

About c3:
Remember that c3 is only a reference. c1 and c2 were referencing an Object but there was no object for c3. Therefor there is no object to be GC'ed. If c3 was referencing to an object then it would also be eligible.

Next time post the question so that others that don't have the book could help you.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Ankit Garg

Joined: Aug 03, 2008
Posts: 9374

wei not everyone has that book and even if they have the book, they might not have the book with them all the time. In the future you can get better response if you provide the question with which you are confused. This provides an opportunity for everyone to help you and also helps them to understand the topic and increase their own knowledge...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Michelle Chang

Joined: Jan 22, 2010
Posts: 8
Along the same line, my question for Q1 in chapter 3 is that if the local variable cb is also eligible for GC? why or why not? Thanks.

Below is the question again:

When // doStuff is reached, how many objects are eligible for GC?
A. 0
B. 1
C. 2
D. Compilation fails
E. It is not possible to know
F. An exception is thrown at runtime
✓ C is correct. Only one CardBoard object (c1) is eligible, but it has an associated Short
wrapper object that is also eligible.
A, B, D, E, and F are incorrect based on the above. (Objective 7.4)

Always speak up for stupid questions.
Devaka Cooray
ExamLab Creator

Joined: Jul 29, 2008
Posts: 3528

Shifong Chang wrote: my question for Q1 in chapter 3 is that if the local variable cb is also eligible for GC? why or why not?

Not variables are eligible for the GC. Only the objects, which are *not* referred by any variable, are eligible for the GC. The object passed to the go(~) method (line-11) is already referenced by'c2' - hence, it is not eligible for the GC.

Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
KrishnaPrasad raghavan
Ranch Hand

Joined: Oct 28, 2008
Posts: 46
So c1 and c3 are eligible for GC. Am i correct here?.

The cb within the object is not eligible for gc even though it is made to refer to null.

The cb here won't be eligible for GC.. is it ?.
Devaka Cooray
ExamLab Creator

Joined: Jul 29, 2008
Posts: 3528

Only 'c1' and 'story' ('story' of 'c1') is eligible for the GC.
I agree. Here's the link:
subject: Object eligable to GC
It's not a secret anymore!