+ 1
Value in switch case - java
Hi guys, Do you know how (and do I?) can I use variable as a switch case in java? I want to do sth like switch (someval) { case 1: break; case b: break; } I tried to declare b as a final but I didn't work and it says me b has to be constant
1 Respuesta
+ 2
What type is someval?
If you can provide the code, that would be helpful.
EDIT: sorry, I misunderstood the question. The value b has to be a constant literal AFAIK (something like 5, 8138, 27, "string", 2.5, 9.283f, 29294948282838L).
EDIT 2: if you declare a final variable and initialize it with a value at the declaration point, it should be possible to use it in a case expression. https://stackoverflow.com/questions/16255270/final-variable-case-in-switch-statement