I know that this = null
is illegal.
I'm wondering if there's some other way to have an object clean itself up.
my desire is to be able to do something like this:
A a = new A();
a.doStuffAndDisappear();
if(a == null){
//this is true after my doStuffAndDisappear() method.
}
I suspect there's no way to make this happen, but thought it would be worth asking.
That is not possible, I would think. If
a
is an instance variable of some objectx
,doStuffAndDisappear
somehow could get a reference tox
and call a setter to seta
to null. Better not to go that route as it is totally counter intuitive.Best would be to clean up in
doStuffAndDisappear
(well, cant make is disappear) and make sure that no one else is referring is toa
. GC should take care of rest.I might be missing something but, since you mention that:
And:
How about, not keeping a reference to the object at all?
Update
Since this is not what the OP is looking for, let me expand @Perce solution:
Now you don't need to know the type of the parent or the field that holds
A
.Working Example.
If you want to make
A
null its reference at every parent, just changeA
to hold a list of parents (List<AParent> parents
), and implement a method to track parents:Plus a
void cleanUp()
which iterate over its parents setting null:Notice how this solution looks a lot like JavaBeans Bound Properties... This is not by coincidence; we are, in a way, creating a simple Event / Listener architecture to that
A
can notify its parents to get ride of their reference to it.Yes, it is possible if you reimplement GC in your applications. My answer is inspired by your discussion with Patricia, where you said you want to have all references to that object set to null. GC example:
This way you can control that there are no other refernces to your class. Your class must also contain the refernces to your GC memory only. Your example:
As everyone else has said, this simply isn't possible. If it's cleaning up resources you're after, then you might consider using a pattern such as:
And then using it like so:
A better solution might be to implement the
java.io.Closeable
orjava.lang.AutoCloseable
interfaces depending on your circumstance:In which case you can use a try-with-resources statement:
Or you could even combine the two and do it that way, whichever you prefer.
Or lastly you could do it the way William Morrison explained (though I'd probably cheat and just use
java.util.concurrent.atomic.AtomicReference
instead of making my own class, and it comes with the added benefit of being a generified type), which, depending on your circumstance, may really be unnecessary. After all, you could always just do (even though it might seem a little odd):You can't have an object set another object's reference to null like this without having them both aware of one another. Doing what you want is impossible without some legwork. I advise against the following example.
This will cause one value's reference to a second reference to vanish as a second reference is setting the first reference's reference to null.
This neat question is technically interesting. However, considering your intent: You are attempting to force the condition of all references being
null
so that you can guard against the invalid use of an otherwise expired instance.It is bad practice to assign any meaning to
null
.Instead, modify (or facade if you can't modify) your
A
object so that some other flag is available. For examplepublic boolean isClosed() ...