...
Programmers often incorrectly assume that declaring a field or variable final
makes the referenced object immutable. Declaring variables that have a primitive type to be final
does prevent changes to their values after initialization (by normal Java processing). However, when the variable has a reference type, the presence of a final
clause in the declaration only makes the reference itself immutable. The final
clause has no effect on the referenced object. Consequently, the fields of the referenced object may be mutable. For example, according to the Java Language Specification, §4.12.4, "final
Variables" [JLS 20112013],
If a
final
variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object.
This applies also to arrays
...
because arrays are objects; if a final
variable holds a reference to an array, then the components of the array may still be changed by operations on the array, but the variable will always refer to the same array.
Similarly, a final
method parameter obtains an immutable copy of the object reference. Again, this has no effect on the mutability of the referenced data.
...
When the values of the x
and y
members instance variables must remain immutable after their initialization, they should be declared final
. However, this invalidates a set_xy()
method because it can no longer change the values of x
and y
:
...
With this modification, the values of the instance fields variables become immutable and consequently match the programmer's intended usage model.
...
Code Block | ||
---|---|---|
| ||
final public class Point implements Cloneable { private int x; private int y; Point(int x, int y) { this.x = x; this.y = y; } void set_xy(int x, int y) { this.x = x; this.y = y; } void print_xy() { System.out.println("the value x is: "+ this.x); System.out.println("the value y is: "+ this.y); } public Point clone() throws CloneNotSupportedException{ Point cloned = (Point) super.clone(); // No need to clone x and y as they are primitives return cloned; } } public class PointCaller { public static void main(String[] args) throws CloneNotSupportedException { Point point = new Point(1, 2); // WillIs not be changed in main() point.print_xy(); // Get the copy of original object Point pointCopy = point.clone(); // pointCopy now holds a unique reference to the // newly cloned Point instance // Change the value of x,y of the copy. pointCopy.set_xy(5, 6); // Original value remains unchanged point.print_xy(); } } |
...
Clients can trivially modify the contents of the array , even though declaring the array reference to be final
prevents modification of the reference itself.
...
As before, this method provides direct access to the array objects themselves, which but this is safe because String
is immutable. If the array contained mutable objects, the getItems()
method could return an array of cloned objects instead.
Compliant Solution (Unmodifiable Wrappers)
...
Neither the original array values nor the public
list can be modified by a client. For more details about unmodifiable wrappers, refer to SEC57-J 03. Provide sensitive mutable classes with unmodifiable wrappers. This solution can also be used when the array contains mutable objects.
...
Item 13, "Minimize the Accessibility of Classes and Members" | |
Chapter 6, "Interfaces and Inner Classes" | |
Class Properties for Security Review in an Object-Capability Subset of Java |
...