This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt on GC

 
prathap evs
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we do unreachable Objects become reachable again?

For example:
class test
{
public void method()
{
Sample obj=new Sample("first"); //let 'Sample' is an class
obj=null;
//?? Now I want to point 'obj' to the same object which is pointing before make it as null.
}
}
:roll:
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64851
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"prathap evs", please check your private messages for an important administrative matter.
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could modify your code to override the finalize method of the Sample class which is guaranteed to be called once only, but there is no guarantee that it will indeed ever be called. So, in the finalize method you make the object being finalized as reachable from a reference of class test.


[ December 26, 2008: Message edited by: Harvinder Thakur ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's probably best not to use finalize() at all; it is really only of use to release "native" resources. You can however justify that code as an example of "I just wanted to see what happens if . . ."

Once you have lost object references, you cannot usually retrieve them. The problem in real life is more to do with getting rid of the last reference, so an object can be collected.
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by prathap evs:
Can we do unreachable Objects become reachable again?


I can't imagine why you would want to do that. Can you provide a meaningful reason?

The GC exists to make programming easier. The whole finalize approach, or the C++ style ~constructor has been found to be a bugfarm.

If you want to keep track of something, do so. If not, let the GC handle it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic