saravanan
saravanan

Reputation: 5407

why case constant must be compile time constant in switch

why case constant must be compile time constant in switch?

Upvotes: 1

Views: 1041

Answers (1)

Jon Skeet
Jon Skeet

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

Related Questions