JDK-8146064 : javac allows empty enum inside anonymous class in static method
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 7,8
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: x86
  • Submitted: 2015-12-10
  • Updated: 2016-01-04
  • Resolved: 2016-01-04
Related Reports
Duplicate :  
Description
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 ----------


Comments
Eclipse reports compilation error "The member enum ShouldNotBeAllowed can only be defined inside a top-level class or interface or in a static context", All versions java 7 and java 8 of javac compiler compiles without error. 9 ea build 96 reports compiler issue. Test results 7uxx - Fail (No compilation error) 8uxx - Fail (No compilation error) This include 8u72 -b11 as well. 9 ea -b95 Pass (Compiler error) 9 ea -b96 Pass (Compiler error)
23-12-2015