• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt on GC

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"prathap evs", please check your private messages for an important administrative matter.
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Let's go to the waterfront with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic