enum.values() - is an order of returned enums deterministic

后端 未结 4 1055
傲寒
傲寒 2020-12-07 19:44

I have a enum SOME_ENUM:

public enum SOME_ENUM {
  EN_ONE,
  EN_TWO,
  EN_THREE;
}

Will SOME_ENUM.values() alway

4条回答
  •  我在风中等你
    2020-12-07 20:26

    The Java language specification uses this explicit language:

    @return an array containing the constants of this enum type, in the order they're declared [Source]

    So, yes, they will be returned in declaration order. It's worth noting that the order might change over time if someone changes the class so be very careful about how you use this.

提交回复
热议问题