Anything but return this == other
would be counter intuitive and violate the principle of least astonishment. Two enum constants are expected to be equal
if and only if they are the same object and the ability to override this behavior would be error prone.
Same reasoning applies to hashCode()
, clone()
, compareTo(Object)
, name()
, ordinal()
, and getDeclaringClass()
.
The JLS does not motivate the choice of making it final, but mentions equals in the context of enums here. Snippet:
The equals method in Enum
is a final method that merely invokes super.equals
on its argument and returns the result, thus performing an identity comparison.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…