Jtest logo




Contents  Previous  Next  Index

CODSTA.OVERRIDE


If you override 'Object.equals()', you should also override 'Object.hashCode()'

Description

This rule flags code where a class that overrides 'Object.equals()' does not also override 'Object.hashCode()'.

A class that overrides 'Object.equals()' should also override 'Object.hashCode()'. Containers and other utilities that group or compare objects in ways depending on equality rely on hashcodes to indicate possible equality.

Example

 package CODSTA;
 
 public class OVERRIDE { // violation, no hashCode()
     public Object equals (Object o) {
         // implementations
     }
 }

Repair

Whenever a class overrides an equals() method, it should also override hashCode().

Reference

http://www.infospheres.caltech.edu/resources/code_standards/recommendations.html


Contents  Previous  Next  Index

ParaSoft logo
(888) 305-0041 info@parasoft.com Copyright © 1996-2001 ParaSoft