FULL PRODUCT VERSION :
java version "1.8.0_66"
Java(TM) SE Runtime Environment (build 1.8.0_66-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.66-b17, mixed mode)
A DESCRIPTION OF THE PROBLEM :
An empty (no constants) enum inside an anonymous class is silently accepted by javac when it is within a static method.
If constants are added, a compiler error is issued, but not about the enum itself, but rather about a bad reference to "this" in the constant: "non-static variable this cannot be referenced from a static context".
Note: Eclipse compiler correctly rejects such an enum.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile the given test case source.
Run it.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
A compilation error indicating that enums are not allowed within inner/anonymous classes.
ACTUAL -
Compilation succeeds. The program runs, and shows the following output:
SimpleTest$1
[class SimpleTest$1$ShouldNotBeAllowed]
class java.lang.Enum
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.util.Arrays;
public class SimpleTest {
public static void main(String[] args) {
Object a = new Object() { enum ShouldNotBeAllowed{ } };
Class<?>[] classes = a.getClass().getDeclaredClasses();
System.out.println(a.getClass().getName());
System.out.println(Arrays.toString(classes));
System.out.println(classes[0].getSuperclass());
}
}
---------- END SOURCE ----------
java version "1.8.0_66"
Java(TM) SE Runtime Environment (build 1.8.0_66-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.66-b17, mixed mode)
A DESCRIPTION OF THE PROBLEM :
An empty (no constants) enum inside an anonymous class is silently accepted by javac when it is within a static method.
If constants are added, a compiler error is issued, but not about the enum itself, but rather about a bad reference to "this" in the constant: "non-static variable this cannot be referenced from a static context".
Note: Eclipse compiler correctly rejects such an enum.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile the given test case source.
Run it.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
A compilation error indicating that enums are not allowed within inner/anonymous classes.
ACTUAL -
Compilation succeeds. The program runs, and shows the following output:
SimpleTest$1
[class SimpleTest$1$ShouldNotBeAllowed]
class java.lang.Enum
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.util.Arrays;
public class SimpleTest {
public static void main(String[] args) {
Object a = new Object() { enum ShouldNotBeAllowed{ } };
Class<?>[] classes = a.getClass().getDeclaredClasses();
System.out.println(a.getClass().getName());
System.out.println(Arrays.toString(classes));
System.out.println(classes[0].getSuperclass());
}
}
---------- END SOURCE ----------
- duplicates
-
JDK-8034044 Class.getModifiers() returns "static" for anonymous classes
- Closed