0

I am studying for my Java OCA exam and I am playing with switch statement. As far as I know I can use a final constant variable in a case statement but my example is not compiling because the variable I am not using apparently it is not a constant expression:

public class SwitchOca {
    public static void main(String[] args) {
        final Integer x = Integer.valueOf(0);
        final Integer y = Integer.valueOf(0);
        switch(x) {
            case y:
                System.out.println("error ...");
                break;
            case 0:
                System.out.println("primitive ...");
                break;
        }
    }
}

The output of the compilation says:

javac SwitchOca.java
Picked up _JAVA_OPTIONS: -Xmx1024M
SwitchOca.java:6: error: constant expression required
            case y:
                 ^
1 error

Any help to understand this will be much appreciated.

0 Answers0