DeepSource has joined Harness. Learn more
Java logoJava/
JAVA-W0107

`equals` always returns `false`JAVA-W0107

Major severityMajor
Anti-pattern categoryAnti-pattern

This class defines an equals method that always returns false. This means that an object is not equal to itself, and it is impossible to create useful Maps or Sets of this class.

More fundamentally, it means that equals is not reflexive, one of the requirements of its API.

Equals

Bad Practice

class MyClass {

    @Override
    public boolean equals(Object o) {
        return false;
    }

}
class MyClass {

    @Override
    public boolean equals(Object o) {
        return ...; // return a value based on required criteria for this object.
    }

}

This may be an attempt to reset the semantics of equality for this class; meaning objects of this class will be compared in the same way as those of the Object class. If you need to override an equals method inherited from a different superclass in such a way, you could use something like this:

@Override
public boolean equals(Object o) {
    return this == o;
}

References