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 garbage collection 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 "garbage collection" Watch "garbage collection" New topic

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
Yes the object created at line 2 may be garbage collected, since there are no active references to this object.

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

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;
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.
umang bhartia
Ranch Hand

Joined: Sep 29, 2000
Posts: 60
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.
I agree. Here's the link:
subject: garbage collection
It's not a secret anymore!