Community
Participate
Working Groups
The following code compiles in Eclipse but not under javac. The problem is the parenthesis around NORMAL, under javac (java version "1.5.0_04") this causes a compile time error. According to the JLS 3rd edition (http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.11) it's either a constant expression or an enum constant name. I think javac has the correct behavior (it fails to compile), but a lousy error message. public class EnumError { public enum State { NORMAL } public void foo() { State state = State.NORMAL; switch (state) { case (NORMAL) : System.out.println(State.NORMAL); break; } } }
I would say this is a bug in javac if I refer to jls 15.28 about the constant expression specifications: 15.28 Constant Expression ConstantExpression: Expression A compile-time constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following: ... * Parenthesized expressions whose contained expression is a constant expression. ... So because NORMAL is a constant expression, (NORMAL) should also be a constant expression. If you remove the surrounding parenthesis, it works fine. Philippe, I would close as INVALID.
I would agree with you Olivier. Please add a regression test, and close as invalid.
Added org.eclipse.jdt.core.tests.compiler.regression.EnumTest.test122. Closing as INVALID.
*** Bug 108675 has been marked as a duplicate of this bug. ***
Reopening, pls see last comment Javac matching entryhttp://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6356542 We should reject parenthesized enum constants in switch.
URL is http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6356542
Fixed and released in HEAD. Regression test added in org.eclipse.jdt.core.tests.compiler.regression.EnumTest.test122.
Verified for 3.2 M4 using build I20051212-0010