Hi ranchers,
In the above post, Dont we need to check all the members variables of the class are equal or not along with the object's hashcode to ensure that the two objects of 'SortOf' class are equal??
Please clarify
Thanks in Advance.
It is OK, if you use all the fields of the class in hashCode()
computation and in equals() method. But it is recommended that
your equals() does work best using fewer fields. Suppose a person
has Social Security Number that is unique for that Person.
If you are using that SSN to compare two Person objects, your
equals method gives efficient result. No more matching is required
as well there.
Comparing two Person objects on behalf of their age is inefficient.
You can use more than one field, even all field to compute the
equals() and it may give you more correct result. But we should
try to pick the fields that we think better for use in equals().
And there should be good contract match between equals() and hashCode().
Regards,
cmbhatt