23

Is there any solution for destroying current instance of object in itself ?

I am looking for something which looks like:

class KillMe {
    ....
    public void destroy() {
        this.getObject = null //this is only for demonstrate my idea
    }

    ....
}
gioele
  • 9,748
  • 5
  • 55
  • 80
Smolda
  • 882
  • 5
  • 13
  • 34
  • 4
    Why would you want to? Anyway, java objects cannot be "destroyed" by anything other than the garbage collector. – skaffman Jul 20 '11 at 19:27
  • The object I want to make NULL is BEAN in session scope. I also want to do it in current instance of object. – Smolda Jul 20 '11 at 19:32
  • If this is a Spring question, then phrase it as one, and explain properly what you're trying to achieve. – skaffman Jul 20 '11 at 19:33
  • you cant destroy objects in java, the garbage collector does that when your not using them, and all its references are freed. – raym0nd Jul 20 '11 at 19:34
  • 1
    If this would be possible i could code funny constructors that immediately destroy the object someone wants to create or i could install nice hidden NullPointerExceptions ;) – Tim Schmelter Jul 20 '11 at 19:37
  • I don't think it is a question of which context of object it is. – Smolda Jul 20 '11 at 19:39
  • Tim: u are right :) I did not say it is the way I want to go :] I just wondering if its possible or if is there any other solution. – Smolda Jul 20 '11 at 19:41
  • @Tim that would be evil and awesome. @Smolda Is there any reason you can't just say `object = null`? – Shaded Jul 20 '11 at 19:41
  • @Shaded I can but I need to be sure if there does not exist another way. – Smolda Jul 20 '11 at 19:49

4 Answers4

29

Java objects live until there are no longer any references to them. Since an object cannot determine how many references there are to itself, an object cannot "destroy itself".

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
4

The object doesn't have access to the references to it so there's no way to set them to null or something else. An object can only be "destroyed" when the garbage collector comes around and clears out all unreferenced objects.

That being said, try this:

public void destroy() {
    System.exit( 0 );
}
tskuzzy
  • 35,812
  • 14
  • 73
  • 140
3

No, objects cannot suicide. Any reference of itself is just a reference.

To "clear" the object within itself one would just clear all instance variables.

To "clear" the object outside itself one would set the variable equal to null.

Joseph Hansen
  • 12,665
  • 8
  • 50
  • 68
0

Your example of what you want to do demonstrates a lack of understanding of how/when objects are destroyed/cleaned up.

class KillMe {
    ....
    public void destroy() {
        this.getObject = null //this is only for demonstrate my idea
    }
}

The call you have there (this.getObject) gets a reference to the current object, and then sets that reference to null (ie, no longer pointing at the current object. It doesn't do anything to the object itself, nor does it change other variables elsewhere in the code that happen to point at the object.

Consider it this way... variables point at values (objects). Until no more variables point at a given value, that value is kept around (not garbage collected/destroyed). In order to remove all variable references to a value, you need to find all the variables that point at it and point them at something else (ex, null). Realistically, you can't do this (though I suppose nothing is impossible with enough scary reflection and/or bytecode magic ;)

To think of it another way, if you were able to destroy the value/object itself, what would happen to all the other variables that are currently pointing at it? Do they now point at freed memory, resulting in some Java version of a segfault?

Velimir Mlaker
  • 10,664
  • 4
  • 46
  • 58
RHSeeger
  • 16,034
  • 7
  • 51
  • 41