Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Howto delete all object pointer in java

Tags:

java

is there an elegant way in Java to set all existing object-pointers to null? I want an object to be deleted, but it is registered in several places an Event Listener.

like image 246
Skip Avatar asked Aug 02 '11 19:08

Skip


People also ask

How does Java get rid of pointers?

So overall Java doesn't have pointers (in the C/C++ sense) because it doesn't need them for general purpose OOP programming. Furthermore, adding pointers to Java would undermine security and robustness and make the language more complex.

How do you clear an object in Java?

The Java. util. Properties. clear() method is used to remove all the elements from this Properties instance.

How do I remove an object from a list in Java?

There are two ways to remove objects from ArrayList in Java, first, by using the remove() method, and second by using Iterator. ArrayList provides overloaded remove() method, one accepts the index of the object to be removed i.e. remove(int index), and the other accept objects to be removed, i.e. remove(Object obj).


2 Answers

You can take a look at References. If you use a reference to refer to the class, it will not prevent an object from being garbage collected when all of the regular references are removed. The references held by the Reference class doesn't count when the collector determines which objects to collect. Of course, the listeners will have to correctly handle the reference suddenly disappearing. And you'll have to make sure something's keeping a regular reference for as long as you need to class to stick around.

Check out this thread for a discussion about the differences between the soft and weak references. What is the difference between a soft reference and a weak reference in Java?

like image 67
sblundy Avatar answered Sep 22 '22 14:09

sblundy


There is no elegant way for that. Don't try to do it or you will end up with difficult to maintain code.

like image 27
Michał Šrajer Avatar answered Sep 21 '22 14:09

Michał Šrajer