Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How Unreachable Object Becomes Reachable again

 
srujankumar kundurthi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to One and All


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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I earlier wrote: . . . I shall move this discussion.
I see somebody else moved it before I had the chance to. Thank you.
 
Shahid Pathan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object is reachable only if application have at least a single reference(without null), if object does not have reference in application then it wont possible to make it reachable again.
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Joanne. I forgot about the finalize() method.
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Thank you, Joanne. I forgot about the finalize() method.


:-) That makes 2 of us!
 
Rob Spoor
Sheriff
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Three
 
Stephan van Hulst
Bartender
Pie
Posts: 5893
63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

[edit]

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.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java Language Specification would use the term "finalizer reachable" here - "phantom reachable" is a more specific concept which only applies of you're actually using a PhantomReference.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic