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);
}
About Question enthuware.ocpjp.v11.2.3400 :
Moderator: admin
-
- Posts: 10
- Joined: Fri Nov 18, 2016 5:48 pm
- Contact:
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v11.2.3400 :
The objective here is to apply the Java Security Guidelines for the purpose of the exam and not to design a fool proof system where all the requirements are explicitly given.
In this case, all you want to do is to ensure that the vertices array immutable. Yes, it is true that Point objects themselves can change but that is not important for the purpose of this question.
In real world application, one would know what level of immutability is required, and may be there, even the Point class may be required to be made immutable. Depends on requirements.
For the purpose of the exam, just apply the security guidelines as given here: https://www.oracle.com/java/technologie ... guide.html
Do not overthink it.
In this case, all you want to do is to ensure that the vertices array immutable. Yes, it is true that Point objects themselves can change but that is not important for the purpose of this question.
In real world application, one would know what level of immutability is required, and may be there, even the Point class may be required to be made immutable. Depends on requirements.
For the purpose of the exam, just apply the security guidelines as given here: https://www.oracle.com/java/technologie ... guide.html
Do not overthink it.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 126 guests