Manual cloning

A quick approach consists of adding a method that copies the current Point to a new Point manually (this is a shallow copy):

public Point clonePoint() {
Point point = new Point();
point.setX(this.x);
point.setY(this.y);

return point;
}

The code here is pretty simple. Just create a new instance of Point and populate its fields with the fields of the current Point. The returned Point is a shallow copy (since Point doesn't depend on other objects, a deep copy will be exactly the same) of the current Point:

Point point = new Point(...);
Point clone = point.clonePoint();
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset