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
Thanks in Advance
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.
You have posted on an inappropriate forum, so I shall move this discussion.
srujankumar kundurthi wrote:
I am new to Java. I know that an Object becomes unreachable if its reference points to null.
This does not really make sense. An Object can have many references to it or non at all. Once it has no references to it, it becomes eligible for garbage collection.
If you have a variable referencing an Object and you set that variable to null then if there are no other references to it then it becomes eligible for garbage collection.
but i came to know that Unreachable Object can be Reachable again.
How did you 'came to know that Unreachable Object can be Reachable again' because it is not true. Please post your source.
Can Any Body Tell me how it's going to possible
Thanks in Advance
Edit: If I was any slower I would be answering this tomorrow!
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.
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.