The reason why it accepts only compile-time constants has to do with how the switch statement works under the covers. It enables the
Java compiler and JIT to implement the switch via a
branch table. The constants must be known at compile time for the compiler to be able to set up the branch table.
Logically, it does the same as multiple if ... else if ... statements. Under the covers, it doesn't work in the same way. A switch statement is probably more efficient than a chain of if ... else if ... statements.
I wouldn't try to force my code into a switch statement just because I suspect that it is faster - that's
premature optimization.