开发者

passing enums by ref in java

开发者 https://www.devze.com 2023-01-21 03:54 出处:网络
How 开发者_如何学编程can i pass enum parameter by reference in java? Any solution?Java is pass by value - always.

How 开发者_如何学编程can i pass enum parameter by reference in java? Any solution?


Java is pass by value - always.

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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号