Reputation: 5407
why case constant must be compile time constant in switch?
Upvotes: 1
Views: 1041
Reputation: 1502825
The bytecode format requires it to be a constant, for one thing.
Also, if it's a constant the compiler can check that all the values are different - you can't have the same case twice. You can't do that if the values can change at execution time.
If you don't want to use constants, it's probably best to just use if/else.
Upvotes: 5