aspose file tools*
The moose likes Beginning Java and the fly likes Refference variables and garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Refference variables and garbage collection" Watch "Refference variables and garbage collection" New topic
Author

Refference variables and garbage collection

Gjorgi Var
Ranch Hand

Joined: Feb 24, 2004
Posts: 85
Hi,
If we have two refference variables that are assigned to each other, both will reffer to the same object on the heap. But if we assign one of them to null after we have assigned it to the other variable, wll that object be subjected to garbage collection? Why would it be or would be not?
Tom Henner
Greenhorn

Joined: Jul 04, 2005
Posts: 18
Java passes a copy of the reference value. So setting a to null does not change the reference that b already has. Your object is safe from the gc.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
No, the object is not available for garbage collection until there are NO MORE references to it. This is a very key concept since it is easy to forget to null out all references when you are finished with an object.

Layne


Java API Documentation
The Java Tutorial
 
wood burning stoves
 
subject: Refference variables and garbage collection