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).