This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "garbage collection" Watch "garbage collection" New topic
Author

garbage collection

anil bisht
Ranch Hand

Joined: Nov 04, 2000
Posts: 81
class testfloat
{
public static void main(String []arf){
A a = new A();// line one object
A b = new A();// line 2 object
b = a;// after this line is object created at line 2 eligible for gc ???
a = null;
}
}
vadiraj vd
Ranch Hand

Joined: Dec 15, 2000
Posts: 68
Anil,
Yes the object created at line 2 may be garbage collected, since there are no active references to this object.
regards
vadiraj


Regards<BR>---------<BR>vadiraj<P><BR>*****************<BR>There's a lot of I in J.<BR>*****************
vishad patel
Greenhorn

Joined: Dec 05, 2000
Posts: 17
hi friend
in the given coding the refrence variable at line 2 is not claimed for garbage collection. because it can be reachable through the refrence varible store in the refrence variable b.
if you write the coding in the following manner it will be eligible for GC.
class testfloat{
public static void main(String args[]){
A a = new A();
A b = new A();
a = null;
}
}
Thanks.
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
vishad, I don't agree with you. The object that b references in line 2 doesn't have any references once you say that b=a. So that object can be gc'd after you assign b to a. The object that a references is still not eligble for gc after you set it to null because b is now looking at that item also.
Bill
umang bhartia
Ranch Hand

Joined: Sep 29, 2000
Posts: 60
Anil,
the object created at line 2 will b eligible for GC, as u have asigned a new reference to object b, so now there is no reference to the object which has been created at line 2. This is the reason that it will b eligible for GC.
 
Don't get me started about those stupid light bulbs.
 
subject: garbage collection