Enum values().length vs private field

后端 未结 4 1697
伪装坚强ぢ
伪装坚强ぢ 2020-12-08 13:12

I have enumeration like this:

public enum Configuration {
    XML(1),
    XSLT(10),
    TXT(100),
    HTML(2),
    DB(20);

    private final int id;
    pri         


        
4条回答
  •  粉色の甜心
    2020-12-08 13:26

    I would recommend using values().length. This is far more elegant and the performance overhead versus using a constant will be negligable. Also, you eliminate the risk of the constant ever becoming out of step with the actual length of the enumeration.

提交回复
热议问题