File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Refference variables and garbage collection

Gjorgi Var
Ranch Hand

Joined: Feb 24, 2004
Posts: 85
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

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.


Java API Documentation
The Java Tutorial
I agree. Here's the link:
subject: Refference variables and garbage collection
It's not a secret anymore!