wood burning stoves 2.0*
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 Soft Skills 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
Author

Objects eligible for GC

Kamil Wojcik
Ranch Hand

Joined: May 19, 2008
Posts: 78

Hi,

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

Aakash,
Thank You for that pic, it's really helpful. Now it's clear!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Objects eligible for GC