passing enums by ref in java

前端 未结 2 1883
慢半拍i
慢半拍i 2021-01-17 18:39

How can i pass enum parameter by reference in java? Any solution?

2条回答
  •  梦谈多话
    2021-01-17 19:10

    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.

提交回复
热议问题