What is the best way to override equals method in java to compare more than one field? For example, I have 4 objects in the class, o1, o2, o3, o4 and I want compare all of them with the passed object to the equals method.
if (o1 != null && o2 != null && o3 != null && o4 != null && obj.o1 != null
&& obj.o2 != null && obj.o3 != null && obj.o4 != null
&& o1.equals(obj.o1) && o2.equals(obj.o2) && o3.equals(obj.o3) && o4.equals(obj.o4)) {
do something
}
The problem with this code is that it's not clear and can't be modified easily specially if we have more fields. Is there a better way to achieve that?