是否可以将Object本身设置为null? [英] Is it possible to set an Object to be null from within itself?

查看:109
本文介绍了是否可以将Object本身设置为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 this = null 是非法的。

我想知道是否还有其他办法有一个物体清理自己。

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.

推荐答案

不,因为 a 是一个参考(不是像这个问题标题中的对象)并且没有方法可以修改引用的值,除了定义它的方法(我假设从代码上下文 a 是一个局部变量。)

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).

由于Java没有pass-by-reference,你提出的问题无法完成:没有办法收集地址的引用,以便管理指向的地址。您可以使用包装器对象,但不确定它是什么意思。

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.

这篇关于是否可以将Object本身设置为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆