Search:     Advanced search
server monitoring

Object Model Violation: Just One of equals() and hashCode() Defined

Article ID: 69
Last updated: 06 May, 2008
Views: 279
Posted: 06 May, 2008
by: Tech Pubs S.
Updated: 06 May, 2008
by: Tech Pubs S.

Object Model Violation: Just One of equals() and hashCode() Defined

Abstract

This class overrides only one of equals() and hashCode().

Description

Java objects are expected to obey a number of invariants related to equality. One of these invariants is that equal objects must have equal hashcodes. In other words, if a.equals(b) == true then a.hashCode() == b.hashCode().

Failure to uphold this invariant is likely to cause trouble if objects of this class are stored in a collection. If the objects of the class in question are used as a key in a Hashtable or if they are inserted into a Map or Set, it is critical that equal objects have equal hashcodes.

Examples

The following class overrides equals() but not hashCode().

 public class halfway() {
public boolean equals(Object obj) {
...
}
}
This article was:   Helpful | Not Helpful
Prev   Next
J2EE Bad Practices: getConnection()     Often Misused: Authentication