Enums defined with Array Constant in java

前端 未结 2 395
無奈伤痛
無奈伤痛 2021-01-24 03:12

I\'d like to know if it is possible to define an enum with an Array as a constant; see the following code excerpt.

This does not compile with an illegal start of express

相关标签:
2条回答
  • 2021-01-24 03:22

    Yes, you'll just need to initialize them. For example,

    NICKLE(5, new string[]{"five"})
    
    0 讨论(0)
  • 2021-01-24 03:37

    For this use case, I would use a vararg approach instead:

    public enum Currency {
        PENNY(1, "one", "oneone"),
        NICKLE(5, "five"),
        DIME(10, "ten"),
        QUARTER(25, "twentifive");
    
        private int valueInteger;
        private String[] valueString;
    
        private Currency(int valueInteger, String... valueString) {
            this.valueInteger = valueInteger;
            this.valueString = valueString;
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题