简体   繁体   English

在java中通过ref传递枚举

[英]passing enums by ref in java

How can i pass enum parameter by reference in java? 如何在java中通过引用传递enum参数? Any solution? 有解决方案吗

Java is pass by value - always. Java始终是值得传递的。

You pass references, not objects. 您传递引用,而不是对象。 References are passed by value. 引用按值传递。

You can change the state of a mutable object that the reference points to in a function that it is passed into, but you cannot change the reference itself. 您可以在传入的函数中更改引用所指向的可变对象的状态,但不能更改引用本身。

In Java you cannot pass any parameters by reference. 在Java中,您无法通过引用传递任何参数。

The only workaround I can think of would be to create a wrapper class, and wrap an enum. 我能想到的唯一解决方法是创建一个包装类,并包装一个枚举。

public class EnumReference {
    public YourEnumType ref;
}

And then you would use it like so: 然后你会像这样使用它:

public void someMethod(EnumReference reference) {
    reference.ref = YourEnumType.Something;
}

Now, reference will contain a different value for your enum; 现在,引用将包含枚举的不同值; essentially mimicking pass-by-reference. 基本上模仿传递引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM