Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

equals() generated by Eclipse: getOuterType()?

Tags:

java

eclipse

I have simple class Point with two fields of type double. I asked Eclipse 3.6 to generate equals() and hashCode() for it. The equals() method looks like this:

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Point other = (Point) obj;
    if (!getOuterType().equals(other.getOuterType()))
        return false;
    if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x))
        return false;
    if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y))
        return false;
    return true;
}

And the getOuterType looks like this:

private Point getOuterType() {
    return Point.this;
}

So the question is: what's the purpose of getOuterType().equals(other.getOuterType()) line?

like image 972
Artem Pelenitsyn Avatar asked Oct 03 '10 16:10

Artem Pelenitsyn


People also ask

What is hashCode and equals contract?

The general contract of hashCode is: During the execution of the application, if hashCode() is invoked more than once on the same Object then it must consistently return the same Integer value, provided no information used in equals(Object) comparison on the Object is modified.


1 Answers

Well, if your class is an inner class (non-static nested class), it has an outer, enclosing instance. Two objects of an inner class type aren't really equal unless the enclosing instances are equal, too; the outer instance like a hidden field (usually named this$0 by javac).

like image 76
Chris Jester-Young Avatar answered Oct 08 '22 04:10

Chris Jester-Young