Can I set enum start value in Java?

前端 未结 9 1651
长情又很酷
长情又很酷 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 19:08

    If you want emulate enum of C/C++ (base num and nexts incrementals):

    enum ids {
        OPEN, CLOSE;
        //
        private static final int BASE_ORDINAL = 100;
        public int getCode() {
            return ordinal() + BASE_ORDINAL;
        }
    };
    
    public class TestEnum {
        public static void main (String... args){
            for (ids i : new ids[] { ids.OPEN, ids.CLOSE }) {
                System.out.println(i.toString() + " " + 
                    i.ordinal() + " " + 
                    i.getCode());
            }
        }
    }
    
    OPEN 0 100
    CLOSE 1 101
    
    0 讨论(0)
  • 2020-11-30 19:09
     public class MyClass {
        public static void main(String args[]) {
         Ids id1 = Ids.OPEN;
         System.out.println(id1.getValue());
        }
    }
    
    enum Ids {
        OPEN(100), CLOSE(200);
    
        private final int id;
        Ids(int id) { this.id = id; }
        public int getValue() { return id; }
    }
    

    @scottf, You probably confused because of the constructor defined in the ENUM.

    Let me explain that.

    When class loader loads enum class, then enum constructor also called. On what!! Yes, It's called on OPEN and close. With what values 100 for OPEN and 200 for close

    Can I have different value?

    Yes,

    public class MyClass {
        public static void main(String args[]) {
         Ids id1 = Ids.OPEN;
         id1.setValue(2);
         System.out.println(id1.getValue());
        }
    }
    
    enum Ids {
        OPEN(100), CLOSE(200);
    
        private int id;
        Ids(int id) { this.id = id; }
        public int getValue() { return id; }
        public void setValue(int value) { id = value; }
    }
    

    But, It's bad practice. enum is used for representing constants like days of week, colors in rainbow i.e such small group of predefined constants.

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

    whats about using this way:

    public enum HL_COLORS{
              YELLOW,
              ORANGE;
    
              public int getColorValue() {
                  switch (this) {
                case YELLOW:
                    return 0xffffff00;
                case ORANGE:
                    return 0xffffa500;    
                default://YELLOW
                    return 0xffffff00;
                }
              }
    }
    

    there is only one method ..

    you can use static method and pass the Enum as parameter like:

    public enum HL_COLORS{
              YELLOW,
              ORANGE;
    
              public static int getColorValue(HL_COLORS hl) {
                  switch (hl) {
                case YELLOW:
                    return 0xffffff00;
                case ORANGE:
                    return 0xffffa500;    
                default://YELLOW
                    return 0xffffff00;
                }
              }
    

    Note that these two ways use less memory and more process units .. I don't say this is the best way but its just another approach.

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