Override equals and hashCode methods

If you want to override equals method in your class, you should also override hashCode method as well. There are a lot of ways the two methods can be implemented incorrectly.

I found the easiest way is to use the wizard in Eclipse, just running Source | Generate hashCode() and equals()... either from menu or context menu. For example, this is what Eclipse generated for a simple class Scrap with a instance variable number:

/* (non-Javadoc)
* @see java.lang.Object#hashCode()
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + number;
return result;

/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Scrap other = (Scrap) obj;
if (number != other.number)
return false;
return true;
I don't feel I need to modify anything in the 2 generated methods. I especially like the @Override annotation being used in the generated code. It clearly documents the method overriding and enforces the compile-time check.


Tobi ZHAW said...

I realy wonder why eclipse thinks, that the other.number is declared as public! Shouldn't it be something like other.getNumber() ?

That was one of the mistakes, why I had a lot of problems in my JSF/Hibernate web application...

javahowto said...

other.number doesn't have to be public. It works equally well even when it's private. When we get to this statement line, this and other are of the exactly same class and other is accessing its private member variables inside its own class.

If you use field-based access mode in your persistence, there should be no problem. But for property-based access mode, this type of direct access may confuse the persistence framework.

Anonymous said...

is there an eclipse plugin for this?

Javin @ FIX Protocol Tutorial said...

Nice article but I believe its important to understand the consequences of not following this contract as well and for that its important to understand application of hashcode in collection classes e.g. How HashMap works in Java and how hashcode() of key is used to insert and retrieve object from hashMap.


Anonymous said...

Nice article , you have indeed cover the topic with great details. I have also blogged my experience on java 5 tips to write equals method in Java . let me know how do you find it.

Ivanempire1 said...
This comment has been removed by the author.
Steve Smith said...

Great and Useful Article.

Online Java Training

Java Online Training India

Java Online Course

Java EE course

Java EE training

Best Recommended books for Spring framework

Java Interview Questions

Java Course in Chennai

Java Online Training India

eMexo Technologies said...

Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

Internet Of Things training in electronic city