java 是否可以将类中的类的实例设置为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16924116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:23:58  来源:igfitidea点击:

Is it possible to set to null an instance of a class within the class

javagarbage-collection

提问by eBehbahani

Is it possible to set to null an instance of a class within the class. For example, could I do something like this

是否可以将类中的类的实例设置为 null。例如,我可以做这样的事情吗

int main{
    //Create a new test object
    Test test = new Test();
    //Delete that object. This method should set the object "test" to null, 
    //thus allowing it to be called by the garbage collector.
    test.delete();

}


public class Test{

    public delete(){
        this = null;
    }
}

I have tried this and it does not work. Using "this = null" I get the error that the left hand side needs to be a variable. Is there a way to achieve something similar?

我试过这个,但它不起作用。使用“this = null”我得到左侧需要是变量的错误。有没有办法实现类似的目标?

回答by RichieHindle

An instance of an object doesn't know which references might be referring to it, so there's no way that code within the object can null those references. What you're asking for isn't possible(*).

对象的实例不知道哪些引用可能引用它,因此对象中的代码无法将这些引用设为空。你所要求的是不可能的(*)。

*at least not without adding a pile of scaffolding to keep track of all the references, and somehow inform their owners that they should be nulled - in no way would it be "Just for convenience".

*至少不是不添加一堆脚手架来跟踪所有引用,并以某种方式通知它们的所有者它们应该被清零——绝不是“只是为了方便”。

回答by Zim-Zam O'Pootertoot

You can do something like this

你可以做这样的事情

public class WrappedTest {
    private Test test;
    public Test getTest() { return test; }
    public void setTest(Test test) { this.test = test; }
    public void delete() { test = null; }
}

回答by stinepike

"this" is a final variable. you can not assign any values to it.

" this" 是最终变量。您不能为其分配任何值。

If you want to set the reference null you can do this

如果你想设置引用为空,你可以这样做

test = null;

回答by gparyani

thisis a reference to the instance of your class. When you modify a reference variable, it only modifies thatreference and nothingelse. For example:

this是对您的类的实例的引用。当您修改引用变量时,它只会修改引用,而不会修改其他任何内容。例如:

Integer a = new Integer(1);
Integer b = a;

a = new Integer(2);      //does NOT modify variable b

System.out.println(b);   //prints 1

回答by pinkpanther

Is it possible to set to null an instance of a class within the class?.

Is it possible to set to null an instance of a class within the class?.

You cannot do this from the member methods of the same instance. So, this=nullor that sort of thing will not work.

您不能从同一实例的成员方法中执行此操作。所以,this=null或者那种事情是行不通的。

How come one set an instance to an null?

为什么将实例设置为空?

That question itself is wrong, we set references to nullbut not instances. Unused objects automatically garbage collected in java.

这个问题本身是错误的,我们设置了引用null而不是实例。未使用的对象在 java 中自动垃圾收集。

If you set test=nullit will eventually gets garbage collected.

如果你设置test=null它最终会被垃圾收集。

 int main{
    //Create a new test object
    Test test = new Test();
    // use the object through test
    test=null;
}