P
Philipp
Hello,
I don't understand why the following code gives a compilation error. In
the "case" statements, the compiler complains that "Case expression must
be constant expression", but as far as I can see, the expression are
constant.
Thanks for any comments
Phil
--- SSCCE ---
public class SwitchTest {
public final int a = 1;
public final int b = 2;
public final int c = 3;
public SwitchTest() {}
public static void main(String[] args) {
SwitchTest object = new SwitchTest();
int choice = 2;
switch(choice){
case object.a:
System.out.println("a");
break;
case object.b:
System.out.println("b");
break;
case object.c:
System.out.println("c");
break;
default:
System.out.println("other");
}
}
}
I don't understand why the following code gives a compilation error. In
the "case" statements, the compiler complains that "Case expression must
be constant expression", but as far as I can see, the expression are
constant.
Thanks for any comments
Phil
--- SSCCE ---
public class SwitchTest {
public final int a = 1;
public final int b = 2;
public final int c = 3;
public SwitchTest() {}
public static void main(String[] args) {
SwitchTest object = new SwitchTest();
int choice = 2;
switch(choice){
case object.a:
System.out.println("a");
break;
case object.b:
System.out.println("b");
break;
case object.c:
System.out.println("c");
break;
default:
System.out.println("other");
}
}
}