aspose file tools*
The moose likes Beginning Java and the fly likes Doubt on GC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt on GC" Watch "Doubt on GC" New topic
Author

Doubt on GC

prathap evs
Greenhorn

Joined: Dec 24, 2008
Posts: 2
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

Joined: Jan 10, 2002
Posts: 61413
    
  67

"prathap evs", please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
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 ]

thanks
Harvinder
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39380
    
  28
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

Joined: Aug 11, 2007
Posts: 4659
    
    5

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt on GC