Incorrect hashCode() Spelling

ID

java.hashcode_signature_correct

Severity

info

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

reliability

Description

Reports methods named hashcode() (lowercase 'c') that were likely intended to override Object.hashCode().

Rationale

Java method names are case-sensitive. A method named hashcode() does not override Object.hashCode(), so hash-based collections like HashMap and HashSet will use the default identity-based hash code instead of the custom implementation. This is almost always a typo.

// Bad -- does not override Object.hashCode()
public class Account {
    public int hashcode() {
        return Objects.hash(id);
    }
}

Remediation

Rename the method to hashCode() with a capital 'C', and add the @Override annotation to catch such mistakes at compile time.

// Good
public class Account {
    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}