The JLS says that a case label needs to be a ConstantExpression. The yield of ConstantExpression does not include expressions of the form "this.value". Nevertheless, javac compiles the following program without complaint:
public
class Case {
static final int value = 100;
void method(int i) {
switch (i) {
case this.value :
System.out.println("hi");
break;
}
}
}
This error allows some incorrect code to compile.
public
class Case {
static final int value = 100;
void method(int i) {
switch (i) {
case this.value :
System.out.println("hi");
break;
}
}
}
This error allows some incorrect code to compile.
- duplicates
-
JDK-4119776 illegal 'case' constant expressions
- Closed