相关疑难解决方法(0)

switch语句中的最终变量大小写

        final int a = 1;
        final int b;
        b = 2;
        final int x = 0;

        switch (x) {
            case a:break;     // ok
            case b:break;     // compiler error: Constant expression required

        }
        /* COMPILER RESULT:
                constant expression required
                case b:break;
                     ^
                1 error
        */
Run Code Online (Sandbox Code Playgroud)

为什么我会遇到这种错误?如果我愿意final int b = 2,一切正常.

java final case switch-statement

18
推荐指数
2
解决办法
2万
查看次数

标签 统计

case ×1

final ×1

java ×1

switch-statement ×1