About Question enthuware.ocpjp.v11.2.3400 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
alfredo.zuloaga
Posts: 10
Joined: Fri Nov 18, 2016 5:48 pm
Contact:

Re: About Question enthuware.ocpjp.v11.2.3400 :

Post by alfredo.zuloaga »

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);
}

admin
Site Admin
Posts: 9353
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v11.2.3400 :

Post by admin »

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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests