File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Doubt on GC

prathap evs

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
//?? Now I want to point 'obj' to the same object which is pointing before make it as null.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63844

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

[Asking smart questions] [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 ]

Campbell Ritchie

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

Joined: Aug 11, 2007
Posts: 4659

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:
subject: Doubt on GC
jQuery in Action, 3rd edition