• Post Reply Bookmark Topic Watch Topic
  • New Topic

What are some pitfalls that I need to watch out for when overriding the equals method?  RSS feed

 
Mark Richardson
Ranch Hand
Posts: 108
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am learning about overriding the equals method. I got this example code online, but I don't completely trust it. What are some pitfalls I need to watch out for?

 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How long have you got?

That is a correct implementation but not a correctly‑implemented class, but it is prone to errors if you extend from that class. There are all sorts of pitfalls. The first thing I think you shou‍ld do it find out the six things an equals method(←link) must do and the one other thing you must do in that class. When you have read that and convinced yourself your counting is the same as mine, then let's look at the pitfalls.
Fortunately there are several good resources about equals() methods.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!