Here's the code snippet, coded and ran in Eclipse Kelper SR2 with the Java 8 support update installed (retrieved from here: click me):
public class InterfaceSwitchTest {
public enum Enums {
FOO,
BAR;
}
public interface A {
// The below method causes the error.
// Comment out or delete this method and this class will compile.
public default void switchStatement(final Enums x) {
switch (x) {
case FOO:
break;
case BAR:
break;
}
}
// The if statement version of the above.
public default void ifStatement(final Enums x) {
if (x.equals(Enums.FOO))
return;
if (x.equals(Enums.BAR))
return;
}
}
public static class AImplemented implements A {
public AImplemented() {
}
}
public static void main(final String[] args) {
AImplemented instance = new AImplemented();
System.out.print("Yay, no exeptions!");
}
}
Executing the above without commenting out method "switchStatement()" will give the following exception during runtime:
Exception in thread "main" java.lang.ClassFormatError: Illegal field modifiers in class InterfaceSwitchTest$A: 0x100A
Is the above exception expected? The solution to the problem is, of course, to use the if-statement substitute. I'm just wondering why it causes the ClassFormatError?
If it is expected, I would very much like to hear an explanation.
Thanks!
NOTE: I've already setup my eclipse with the java 8 binaries. I have lambda expressions and other defender methods scattered throughout my project, and they run perfectly.