Is it possible to set an Object to be null from wi

2020-06-02 13:53发布

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.

标签: java
7条回答
干净又极端
2楼-- · 2020-06-02 15:00

No, because a is a reference (not an object as in this question's title) and no method can modify the value of a reference except the method in which it is defined (I assume from the code context that a is a local variable).

Since Java doesn't have pass-by-reference, what you ask cannot be done: there's no way to collect addresses-of references in order to manage the addresses pointed to. You might use a wrapper object, but not sure what'd be the point.

查看更多
登录 后发表回答