`equals` method defined for enumerationJAVA-E0096
This enum
defines an overload for the equals
method using the enum
's own class type. The equals()
method of an enum
is not meant to be overloaded (or overridden), and doing so may cause weird bugs to crop up when values of this enum
are compared.
Equality on enumerations is defined using object identity, that is, the address of the object. This works because each variant of an enum
is essentially a static final instance of that enum
class with data corresponding to the declared variant. All usages of an `enum's variants point to the same set of static constants.
Overloading the equals method for an enum
value is exceptionally bad practice since the more specialized overloaded method will always be chosen when comparing enum
variants against each other. Unless the enum
value is cast to Object
before comparison, the original equals
method will not be used.
Bad Practice
enum TestEnum {
First("st"),
Second("nd"),
Third("rd"),
Fourth("th"),
Fifth("th"),
Sixth("th"),
Seventh("th"),
Eighth("th"),
Ninth("th");
String notation;
TestEnum(String n) {
notation = n;
}
// Overload of equals(Object).
public boolean equals(TestEnum other) {
return this.notation == other.notation;
}
}
// ...
assertTrue(TestEnum.Sixth.equals((Object)TestEnum.Ninth)); // False...
assertTrue(TestEnum.Sixth.equals(TestEnum.Ninth)); // True?!
Recommended
Remove the unnecessary equals
method.
enum TestEnum {
First("st"),
Second("nd"),
Third("rd"),
Fourth("th"),
Fifth("th"),
Sixth("th"),
Seventh("th"),
Eighth("th"),
Ninth("th");
String notation;
TestEnum(String n) {
notation = n;
}
}
References
- SpotBugs - EQ_DONT_DEFINE_EQUALS_FOR_ENUM