File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Objects eligible for GC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Objects eligible for GC" Watch "Objects eligible for GC" New topic

Objects eligible for GC

Kamil Wojcik
Ranch Hand

Joined: May 19, 2008
Posts: 78


Source: K&B Self Test Ch3 Q11

Please, I need clear explanation why only ONE object is eligible for GC?

SCJP 6.0 Passed | SCWCD Passed | PSM in progress...
Palash Kumar
Ranch Hand

Joined: Jul 21, 2009
Posts: 33

There are created 4 objects ( using 'new' )

Two of them are of type Beta : b1,b2
Two of them are of type Alpha : a1,a2

Look here, a2 is not null, so you can access its members b1 and b2 ( those which were created above using new )
[a2.b1 is same as a1.b1, since class alpha's b1 is static ]

Now it is clear that, you can access a2,b1 and b2 but you cant access a1 and that is eligible for GC.

Try it with paper and pen digramatically,

SCJP 6 with 80%, going for SCWCD
Aakash Goel
Ranch Hand

Joined: May 26, 2008
Posts: 198
see the image attached... its a bit hazy though.

X -> reference that have been nulled
boxes -> the objects

so the object originally referred by a1 can now not be reached, all others can be reached through a2.

Hope this helps.

[Thumbnail for jr.jpg]

SCJP 5 95%
SCJP FAQ | SCJP Mock Tests | SCJP Tipline | Generics
Kamil Wojcik
Ranch Hand

Joined: May 19, 2008
Posts: 78

Thank You for that pic, it's really helpful. Now it's clear!
I agree. Here's the link:
subject: Objects eligible for GC
It's not a secret anymore!