0

Is it possible to have something along the lines of...

class PrivateStuff {
    [private] void autodestroy() {
        // something something
    }
}

... and have the object be kinda destroyed? Like all references to it be set to null.

P.S. I know you can't really "destroy" an object in Java like you would in other languages (like C), and that it's only killable by the GC, but I'm kinda curious if there's a way to force the GC to kill the caller. Just as an experiment, no intent of use it in a real application.

Alxe
  • 381
  • 3
  • 12
  • Perhaps if the object has a reference to it's parent class, it can call a method there to make it's reference null, which means the GC will collect it. – christopher May 14 '13 at 17:33
  • Or similar as this one: http://stackoverflow.com/questions/171952/is-there-a-destructor-for-java – Trinimon May 14 '13 at 17:35
  • There's no good way to know where all the references are, in the general case. You can rig things so the object is always referenced indirectly, through a "wrapper" object, but the "wrapper" will then hang around until it gets GCed normally. – Hot Licks May 14 '13 at 17:37
  • Thanks for the references. I've looked into them but couldn't satisfy my curiosity, given they were not what I was asking myself. Still useful. – Alxe May 14 '13 at 17:42
  • It is possible for objects which have been discarded to be resurrected as number of ways. This is one of them. AFAIK None are considered a good idea. http://vanillajava.blogspot.co.uk/2012/12/object-resurrection.html – Peter Lawrey May 14 '13 at 17:37

0 Answers0