In java, What does such enum type compile to?

前端 未结 3 766
执笔经年
执笔经年 2020-12-14 10:35

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         


        
相关标签:
3条回答
  • 2020-12-14 10:45

    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 讨论(0)
  • 2020-12-14 11:08

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-14 11:08

    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.

    0 讨论(0)
提交回复
热议问题