Search code examples
javaenums.class-file

Why different class files are created for each enum type if they have constant-specific method?


i have one enum as

enum OperationsType {
  ADD("+"), SUB("-"), DIV("/"), MUL("*");

  private String opcodes;

  private OperationsType(String opcodes) {
    this.opcodes = opcodes;
  }

  public String toString() {
    return this.opcodes;
  }
}

Here all enum type doesn't have any constant Specific method so javac is creating only one class file for enum as

OperationsType.class

but if i add constant Specific method in same code for all enum type then javac is creating 5 class files.

Operations.class
Operations$1.class
Operations$2.class
Operations$3.class
Operations$4.class

for below code

enum Operations {
ADD("+") {
    public double apply(double a, double b) {
        return a + b;
    }
},
SUB("-") {
    public double apply(double a, double b) {
        return a - b;
    }
},
DIV("/") {
    public double apply(double a, double b) {
        return a / b;
    }
},
MUL("*") {
    public double apply(double a, double b) {
        return a * b;
    }
};

private String opcodes;

private Operations(String opcodes) {
    this.opcodes = opcodes;
}

public abstract double apply(double a, double b);
}

So i have doubt that why compiler has created 4 different classes for each enum type if they are having constant Specific method but not creating different classes if they don't have constant Specific method?


Solution

  • Enums with constant-specific methods are implemented using anonymous inner classes. As mentioned in The Java Language Specification:

    The optional class body of an enum constant implicitly defines an anonymous class declaration (§15.9.5) that extends the immediately enclosing enum type. The class body is governed by the usual rules of anonymous classes; in particular it cannot contain any constructors.

    Anonymous inner classes are implemented by creating class files with names like OuterClass$1, OuterClass$2 etc., and this is exactly what happens in the case of the enum.