I am new to Java. I know that an Object becomes unreachable if its reference points to null. but i came to know that Unreachable Object can be Reachable again. Can Any Body Tell me how it's going to possible
You have been given misleading information; an object does not necessarily become unreachable if its reference is set to null. It depends whether there are any other references. Also an object can become unreachable without being set to null, for example if its only reference goes out of scope. I do not believe a truly unreachable object can become reachable again, but it is possible mistakenly to maintain references which one has forgotten about. The WeakReference and simlar classes are designed to guard against that possibility.
James Sabre wrote:How did you 'came to know that Unreachable Object can be Reachable again' because it is not true
There is one way this could happen. When an object's finalize method is called when it is about to be garbage collected, you could create a reference to it from another object. In this situation the object would no longer be garbage collected and would be reachable again.
Joined: Oct 13, 2005
Thank you, Joanne. I forgot about the finalize() method.
I considered the finalize() method, but strictly speaking, at the moment finalize is called, the object is not unreachable. It's merely phantom reachable, but reachable nonetheless.
When an object is truly unreachable (after phantom references have been cleared), it will be reclaimed and there's no way to get it back.
I guess that most texts use the term "unreachable" for objects that are eligible for garbage collection, so here unreachable is any object without a strong reference to them. Using these semantics, an object would indeed become reachable again using the finalize() method.
The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.