Enums defined with Array Constant in java

前端 未结 2 398
無奈伤痛
無奈伤痛 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: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;
        }
    
    }
    

提交回复
热议问题