Why is the final byte not compiled in the case of a switch statement?

byte a = 125;
final byte b = 2;
final Byte c = 3;
switch (a) {
case b: // works fine
    break;
case c: // Constant Expression required
    break;
}

      

Since it c

is a variable final

, isn't it a compile-time constant and therefore a valid case label?

+3


source to share


1 answer


Since c is a final variable, it is not a compile time constant

Not. The rules for constant expressions are given in JLS 15.28 , and they do not include wrapper types:



A constant expression is an expression denoting a primitive type or String value that does not terminate abruptly and is composed using only the following [...]

The wrapper type is not a primitive type and String

.

+5


source







All Articles