wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object for eligible for GC question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object for eligible for GC question" Watch "Object for eligible for GC question" New topic
Author

Object for eligible for GC question

howie jao
Greenhorn

Joined: Nov 08, 2009
Posts: 14
HI, below is a piece of code from the Sierra book. Can anyone explain or show me the step why only CardBoard Object c1 is eligible ?
Also, c1.go(c2) < --- this is passing the copy of c2 object , therefore cb = null is not affecting the originally c2 ??

class CardBoard{
short story = 200;
CardBoard go(CardBoard cb){
cb = null;
return cb;
}

public static void main(String[] s)
{
CardBoard c1 = new CardBoard();
CardBoard c2 = new CardBoard();
CardBoard c3 = c1.go(c2);
c1 = null;
}

Thanks for the help
Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 174
c1 --- o1
c2 --- o2
c3 --- null

at the end
c1 --- null


so only c1 is eligible for GC, GC is done on objects not references.

Here c3 doesn't initialize any object
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

howie jao wrote :

< --- this is passing the copy of c2 object , therefore cb = null is not affecting the originally c2 ??


Here, you are passing a reference of the object of c2, and in the method, go(), you make that reference to null, but the object is referred by c2, so can't be collected by GC.


Hi, UseCodeTag.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Here, you are passing a reference of the object of c2, and in the method, go(), you make that reference to null, but the object is referred by c2, so can't be collected by GC.


Actually I think Jacob is right C3 never is assigned any cardBoard Object and is simply a reference. The C2 passed to c1.go() is a copy only and looses any relationship anyhow once c1 = null. So C1 which was originally assigned a Cardboard Object is set to null in the last line and is legible for GC. C3 has only been a reference pointer, to null , so no object was assigned and thus as areference it is not legible for GC as that is exclusive to Objects on the heap.

Only 2 Cardboard objects were created on the heap, and one was orphaned with a call to null



be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

He, howie jao, asked about object referenced by c2. So......
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Abimaran Kugathasan wrote:He, howie jao, asked about object referenced by c2. So......


I'm not sure what your statement above means but initially, C2 references a new Object on the heap.



When assigning a value to C3 you are using the C1.go() Method and passing a copy of the reference to the object referenced by C2 ( a new CardBoard object);

However the go() method then immediately nullifies the reference, so only the reference C2 remains to the object on the heap and C3 is simply an empty reference so cannot be legible for GC

C1 is then nullified, thus orphaning its object on the heap (no reference now points to the CardBoard Object originally referenced by C1 so that Object becomes legible for garbage collection


There are only ever two CardBoard objects on the heap (not withstanding the class itself) and only two references (C1 & C2) ever point to them. Then C1 is nullified, leaving its object unreferenced.
C3 doesn't point to anything, its nullified by the go() method, regardless of the copy of ref C2 that was passed

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object for eligible for GC question