Constant expressions from Enum

Is there a way to convert enum to constant expression? I want my switch statement to choose among the enumeration values, but I got a compilation error "case expressions must be constant expressions", so I tried to declare them in a variable:

final int REG = MyEnum.REG.getIndex().intValue(); switch (service.getIndex()) { case REG: 

But I still get the same error. According to Oracle documentation http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.28

The compile-time constant expression is an expression denoting a value of a primitive type or String, which does not end abruptly and using only the following:

• Primitive type literals and String literals

So this does not work, because I do not use a literal. I think I will have to declare it as:

 final int REG = 8; 

But it would be much better to associate it with an enumeration. Is there any way to do this?

EDIT

Turns out I don't need to use any kind of final variable. It is as simple as:

 switch (service) { case REG: 

It didn’t come to me until I saw Andrea. Thank you for your responses.

+6
source share
2 answers

If possible, change your getIndex() method to return an enumeration instead of an integer. If this is not possible, you need to match the index of the enumeration element:

Given the following listing:

 public enum Index { ONE, TWO, THREE } 

you can match your index with an enumeration element using

 Index.values()[index] 

Given your Integer getIndex() method, you can do something like

 switch(Index.values()[getIndex()]) case ONE : ... break; case TWO : ... break; case THREE : ... break; } 

Note that if you try to access an index inside an enumeration that is larger than the number of enumeration elements (for example, in the example above, if getIndex() returns> 2), this may cause an ArrayIndexOutOfBoundsException .


I would encapsulate the expression Index.values()[getIndex()] in an enumeration method, for example valueOf(int index) , similar to the default valueOf(String s) . Then you can also handle the correct check of the index of the array (and, for example, return a special enumeration value if the index is out of range). Similarly, you can also convert discrete values ​​that have special values:

 public enum Index { ZERO, ONE, TWO, THREE, REG, INVALID; public static Index valueOf(int index) { if (index == 8) { return REG; } if (index >= values().length) { return INVALID; } return values()[index]; } } 

This is just an example - in any case, it usually depends on the range of values ​​that you get from your getIndex() method, and how you want to match them with enumeration elements.

Then you can use it as

 switch(Index.valueOf(service.getIndex())) { case ZERO : ... break; ... case REG : ... break; ... } 

See also Cast Int for a listing in Java for more information (especially a hint that values() is an expensive operation because it needs to return a copy of the array every time it is called).

+6
source

If you want certain number values ​​assigned to enumeration constants to look like this:

 enum MyReg { REG(8), OTHER(13); public final int value; MyReg(int value) { this.value=value; } } 

Then you use it as follows:

 class Test { public static void main(String[] args) { MyReg reg = MyReg.REG; switch (reg) { case OTHER: System.out.println(reg.value); break; case REG: System.out.println(reg.value); break; } } } 
+2
source

Source: https://habr.com/ru/post/944512/


All Articles