Re: About Question enthuware.ocpjp.v11.2.3400 :
Posted: Fri Apr 09, 2021 9:48 pm
This is not true make clone doesn't work
public static class Shape {
Point[] vertices;
public Shape(Point[] verts) {
this.vertices = verts;
}
public Point[] getVertices() {
return vertices.clone();
}
public void setVertices(Point[] vertices) {
this.vertices = vertices.clone();
}
}
public static void main(String[] args) throws Exception {
Shape shape = new Shape(null);
Point[] vertices = {new Point(1, 2)};
shape.setVertices(vertices);
Point[] vertices1 = shape.getVertices();
vertices[0].x = 12;
System.out.println(vertices[0].x + "--" + vertices1[0].x);
}
public static class Shape {
Point[] vertices;
public Shape(Point[] verts) {
this.vertices = verts;
}
public Point[] getVertices() {
return vertices.clone();
}
public void setVertices(Point[] vertices) {
this.vertices = vertices.clone();
}
}
public static void main(String[] args) throws Exception {
Shape shape = new Shape(null);
Point[] vertices = {new Point(1, 2)};
shape.setVertices(vertices);
Point[] vertices1 = shape.getVertices();
vertices[0].x = 12;
System.out.println(vertices[0].x + "--" + vertices1[0].x);
}