I would like to define some constants like #define myXYZ 1 so I can also use them in switch statements.
When I do the often suggested
public static final Integer myXYZ = 1;
and
case Constants.myXYZ:
I get the compiler message that case expression must be constant
What would be the best way of resolving this?
Thanks!
Just change it to
intand it should be fine:This is because a case value has to be a constant expression or an enum name. From section 15.28 of the JLS (constant expressions):
Therefore an expression of type
intcan be a constant expression, but an expression of typeIntegercan’t.If this is a set of values which makes sense as a concept on its own, then an enum would possibly make more sense.