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 - GC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Objects - GC" Watch "Objects - GC" New topic

Objects - GC

Lovleen Gupta
Ranch Hand

Joined: Feb 26, 2007
Posts: 63
The question is - how many objects are eligible for garbage collection at //doStuff?

My understanding says that there should be 4 objects (c1, c1.story, c2, c2.story). But the answer is 2. Please explain why is it so?

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1710

Hi Guptajee,
You should always see at how many times "new" is being used at first hand.
Inside the main method you created 2 objects; dont forget Short story =5; in the class definition, it is for every object, Short is wrapper not primitive. Inside the go method there wont be affect of making bs =null to the original main reference. It would have local impact at you know copy of the reference is passed to the method so if any internal modification is made to the object as "changing Person's name or age" using passed reference, it would affect the calling method but setting the reference to null in the called method wont affect the calling method object reference; so dont worry about that.

When you set the c1= null; Two objects become eleigible for garbage collection; the first the object referenced by c1 and the object referenced by the story referecen that is encapsulated in the object. So two objects would be eligible for garbage collection.

Hope this helps,

Thanks and Regards,

Rajeswari Kumar

Joined: Mar 23, 2007
Posts: 26
Hi Lovleen
This issue is already discussed. Please check the link

Lovleen Gupta
Ranch Hand

Joined: Feb 26, 2007
Posts: 63
Thanks Chandra & Rajeswari..
It is clear now..!
I agree. Here's the link:
subject: Objects - GC
It's not a secret anymore!