Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Objects eligible for GC

 
Kamil Wojcik
Ranch Hand
Posts: 78
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Source: K&B Self Test Ch3 Q11




Please, I need clear explanation why only ONE object is eligible for GC?
 
Palash Kumar
Ranch Hand
Posts: 33
MyEclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,


 
Aakash Goel
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
jr.jpg
[Thumbnail for jr.jpg]
 
Kamil Wojcik
Ranch Hand
Posts: 78
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aakash,
Thank You for that pic, it's really helpful. Now it's clear!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic