In java, What does such enum type compile to?

6.6k views Asked by At

Below is the code that defines enum type.

enum Company{
    EBAY(30), PAYPAL(10), GOOGLE(15), YAHOO(20), ATT(25);
    private int value;

    private Company(int value){
        super(this.name()); 
        this.value = value;
    }

    public int getValue(){
        return value;
    }
}

that gets internally compiled to,

final class Company extends Enum<Company>{
    public final static Company EBAY = new Company(30);
    public final static Company PAYPAL = new Company(10);
    public final static Company GOOGLE = new Company(15);
    public final static Company YAHOO = new Company(20);
    public final static Company ATT = new Company(25);

    private int value;

    private Company(int value){
        super(this.name(),Enum.valueOf(Company.class, this.name()));
        this.value = value;
    }

    public int getValue(){
        return value;
    }
}

Is my understanding correct?

3

There are 3 answers

3
Elliott Frisch On BEST ANSWER

Functionally, yes. Literally no (you can't explicitly sub-class Enum for one thing). enum(s) have a toString. And your enum isn't valid code (you can't call super()) and getValue needs a return type.

enum Company{
    EBAY(30), PAYPAL(10), GOOGLE(15), YAHOO(20), ATT(25);
    private int value;

    private Company(int value){
        this.value = value;
    }

    public int getValue(){
        return value;
    }
}
7
Filip Bulovic On

If you remove call to super which is illegal and this.name as parameter to super is also illegal, compile it and ran javap on class, this is output:

$ /usr/lib/jvm/java-7-oracle/bin/javap -p Company.class 
Compiled from "Company.java"
final class Company extends java.lang.Enum<Company> {
  public static final Company EBAY;
  public static final Company PAYPAL;
  public static final Company GOOGLE;
  public static final Company YAHOO;
  public static final Company ATT;
  private int value;
  private static final Company[] $VALUES;
  public static Company[] values();
  public static Company valueOf(java.lang.String);
  private Company(int);
  public int getValue();
  static {};
}

Here is bytecode for static, part of it

static {};
  flags: ACC_STATIC
  LineNumberTable:
    line 2: 0
    line 1: 75
  Code:
    stack=5, locals=0, args_size=0
       0: new           #4                  // class Company
       3: dup           
       4: ldc           #8                  // String EBAY
       6: iconst_0      
       7: bipush        30
       9: invokespecial #9                  // Method "<init>":(Ljava/lang/String;II)V
      12: putstatic     #10                 // Field EBAY:LCompany;
0
Jean-François Savard On

Almost, your second snippet does represent well what is internally generated by the compiler (bytecode) however, it is not exactly the same.

A compiled enumeration will contains the ACC_ENUM flag which indicates that this class or its superclass is declared as an enumerated type and will be treated as such by the JVM.

Your second snippet would not (supposing it would compile) include this flag in the bytecode :


ENUM

final class Company extends java.lang.Enum<Company>
      minor version: 0
      major version: 52
      flags: ACC_FINAL, ACC_SUPER, ACC_ENUM

CLASS

final class Company
  minor version: 0
  major version: 52
  flags: ACC_FINAL, ACC_SUPER

As for the rest of your logic (still supposing it would compile) it is right. Internally, an enumeration will be represented as a final class which extends java.lang.Enum. However, note that you can't directly extends java.lang.Enum yourself as this stuff is done by the compiler when creating an enumeration and would result in compilation error if you try to do it yourself.