tags:

views:

49

answers:

1

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?

+1  A: 

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).

Chris Jester-Young
Eclipse seems to think it is an inner class. Otherwise it would not be generating a `getOuterType` method.
Grodriguez
Thanks, Chris! The question actually came from my student, it seems that he really put his class inside some other. (I can't reproduce this on my computer until put the class inside other after your answer.)
Artem Pelenitsyn
Grodriguez, yes, thanks!
Artem Pelenitsyn