No
final Point p = new Point(0,0);
final ImmutablePoint ip = new ImmutablePoint(p);
Two examples:
p.x = 10
ip.getPoint().x = 10
So, first you need to create a protective copy Point taken in the constructor:
public MyImmutablePoint(Point point) {
this.point = new Point(point);
}
Then you need to create a protective copy Pointreturned from the recipient:
public Point getPoint() {
return new Point(point);
}
All this leads me to the assumption that it is probably better not to disclose the inside Point:
public final class MyImmutablePoint {
private final Point point;
public MyImmutablePoint(Point point) {
this.point = new Point(point);
}
public MyImmutablePoint() {
this.point = new Point (0,0);
}
public int getX() {
return point.x;
}
public int getY() {
return point.y;
}
}
.