Why is compareTo on an Enum final in Java?

后端 未结 5 579
感动是毒
感动是毒 2020-12-07 23:36

An enum in Java implements the Comparable interface. It would have been nice to override Comparable\'s compareTo method, but here it\

5条回答
  •  醉梦人生
    2020-12-08 00:23

    Enumeration values are precisely ordered logically according to the order they are declared. This is part of the Java language specification. Therefore it follows that enumeration values can only be compared if they are members of the same Enum. The specification wants to further guarantee that the comparable order as returned by compareTo() is the same as the order in which the values were declared. This is the very definition of an enumeration.

提交回复
热议问题