In
Point(int x, int y) {
x = x;
y = y;
}
You are assigning x
, the local parameter variable, to itself. Same for y
. These are no-ops.
Use
Point(int x, int y) {
this.x = x;
this.y = y;
}
so that you assign the parameter value to the field.
As others have noted, you shouldn't do
Point p = (Point) o;
without knowing if o
is a Point
or not. It will throw a ClassCastException
if it is not assignable to a Point
. Instead use
if (o instanceof Point)
return false;
or
if (o.getClass() != Point.class)
return false;
before casting. Note that the two methods above are not equivalent. You can use the first in most cases, but use the second if Point
is meant to have sub classes.