Can I set enum start value in Java?

前端 未结 9 1650
长情又很酷
长情又很酷 2020-11-30 18:09

I use the enum to make a few constants:

enum ids {OPEN, CLOSE};

the OPEN value is zero, but I want it as 100. Is it possible?

相关标签:
9条回答
  • 2020-11-30 18:48

    @scottf

    An enum is like a Singleton. The JVM creates the instance.

    If you would create it by yourself with classes it could be look like that

    public static class MyEnum {
    
        final public static MyEnum ONE;
        final public static MyEnum TWO;
    
        static {
            ONE = new MyEnum("1");
            TWO = new MyEnum("2");
        }
    
        final String enumValue;
    
        private MyEnum(String value){
            enumValue = value;    
        }
    
        @Override
        public String toString(){
            return enumValue;
        }
    
    
    }
    

    And could be used like that:

    public class HelloWorld{
    
       public static class MyEnum {
    
           final public static MyEnum ONE;
           final public static MyEnum TWO;
    
           static {
              ONE = new MyEnum("1");
              TWO = new MyEnum("2");
           }
    
           final String enumValue;
    
           private MyEnum(String value){
               enumValue = value;    
           }
    
           @Override
           public String toString(){
               return enumValue;
           }
    
    
       }
    
        public static void main(String []args){
    
           System.out.println(MyEnum.ONE);
           System.out.println(MyEnum.TWO);
    
           System.out.println(MyEnum.ONE == MyEnum.ONE);
    
           System.out.println("Hello World");
        }
    }
    
    0 讨论(0)
  • 2020-11-30 18:50

    If you use very big enum types then, following can be useful;

    public enum deneme {
    
        UPDATE, UPDATE_FAILED;
    
        private static Map<Integer, deneme> ss = new TreeMap<Integer,deneme>();
        private static final int START_VALUE = 100;
        private int value;
    
        static {
            for(int i=0;i<values().length;i++)
            {
                values()[i].value = START_VALUE + i;
                ss.put(values()[i].value, values()[i]);
            }
        }
    
        public static deneme fromInt(int i) {
            return ss.get(i);
        }
    
        public int value() {
        return value;
        }
    }
    
    0 讨论(0)
  • 2020-11-30 19:00

    Java enums are not like C or C++ enums, which are really just labels for integers.

    Java enums are implemented more like classes - and they can even have multiple attributes.

    public enum Ids {
        OPEN(100), CLOSE(200);
    
        private final int id;
        Ids(int id) { this.id = id; }
        public int getValue() { return id; }
    }
    

    The big difference is that they are type-safe which means you don't have to worry about assigning a COLOR enum to a SIZE variable.

    See http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html for more.

    0 讨论(0)
  • 2020-11-30 19:00

    Yes. You can pass the numerical values to the constructor for the enum, like so:

    enum Ids {
      OPEN(100),
      CLOSE(200);
    
      private int value;    
    
      private Ids(int value) {
        this.value = value;
      }
    
      public int getValue() {
        return value;
      }
    }
    

    See the Sun Java Language Guide for more information.

    0 讨论(0)
  • 2020-11-30 19:01

    The ordinal() function returns the relative position of the identifier in the enum. You can use this to obtain automatic indexing with an offset, as with a C-style enum.

    Example:

    public class TestEnum {
        enum ids {
            OPEN,
            CLOSE,
            OTHER;
    
            public final int value = 100 + ordinal();
        };
    
        public static void main(String arg[]) {
            System.out.println("OPEN:  " + ids.OPEN.value);
            System.out.println("CLOSE: " + ids.CLOSE.value);
            System.out.println("OTHER: " + ids.OTHER.value);
        }
    };
    

    Gives the output:

    OPEN:  100
    CLOSE: 101
    OTHER: 102
    

    Edit: just realized this is very similar to ggrandes' answer, but I will leave it here because it is very clean and about as close as you can get to a C style enum.

    0 讨论(0)
  • 2020-11-30 19:01

    I think you're confused from looking at C++ enumerators. Java enumerators are different.

    This would be the code if you are used to C/C++ enums:

    public class TestEnum {
    enum ids {
        OPEN,
        CLOSE,
        OTHER;
    
        public final int value = 100 + ordinal();
    };
    
    public static void main(String arg[]) {
        System.out.println("OPEN:  " + ids.OPEN.value);
        System.out.println("CLOSE: " + ids.CLOSE.value);
        System.out.println("OTHER: " + ids.OTHER.value);
    }
    };
    
    0 讨论(0)
提交回复
热议问题