Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Enum class - equals() and hashCode() ?

 
kri shan
Ranch Hand
Posts: 1482
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether enum classes need object's equals() and hashCode() ?
 
Campbell Ritchie
Marshal
Posts: 52636
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start in the Java™ Language Specification. Remember enum members are singletons, so == is adequate as a test for equality.
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Besides, all enum classes implicitly extend java.lang.Enum which has final implementations of both. equals uses == and hashCode uses Object.hashCode. Try overriding them in any enum and you'll get a compiler error.
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!