final variable case in switch statement

后端 未结 3 2044
北海茫月
北海茫月 2020-12-16 12:20
        final int a = 1;
        final int b;
        b = 2;
        final int x = 0;

        switch (x) {
            case         


        
3条回答
  •  -上瘾入骨i
    2020-12-16 13:23

    The case in the switch statements should be constants at compile time. The command

    final int b=2
    

    assigns the value of 2 to b, right at the compile time. But the following command assigns the value of 2 to b at Runtime.

    final int b;
    b = 2;
    

    Thus, the compiler complains, when it can't find a constant in one of the cases of the switch statement.

提交回复
热议问题