Sometimes, when a variable is declared final
, it is believed to be immutable. If the variable is a primitive type, declaring it final
means that its value cannot be changed after initialization (other than through the use of the unsupported sun.misc.Unsafe
class).
subsequently changed. However, if the variable is a reference to a mutable object, the object's contained data members that appears appear to be immutable, may actually in fact be mutable. For example, a final
method parameter field that is stores a reference to an object does not imply immutability of the object itself. The argument to this method uses Similarly, a final
method parameter obtains a copy of the object reference through pass-by-value to copy the reference , but the referenced data remains mutable.
...
... if a
final
variable holds a reference to an array, then the components of the array may be changed by operations on the array, but the variable will always refer to the same array.
Noncompliant Code Example (mutable class, final
reference)
In this noncompliant code example, the values of instance fields a
and b
can be changed even after their initialization. When an object reference is declared final
, it only signifies that the reference cannot be changed, whereas the referenced contents can still be altered.
Code Block | ||
---|---|---|
| ||
class FinalClass {
private int a;
private int b;
FinalClass(int a, int b){
this.a = a;
this.b = b;
}
void set_ab(int a, int b){
this.a = a;
this.b = b;
}
void print_ab(){
System.out.println("the value a is: " + this.a);
System.out.println("the value b is: " + this.b);
}
}
public class FinalCaller {
public static void main(String[] args) {
final FinalClass fc = new FinalClass(1, 2);
fc.print_ab();
// change the value of a,b.
fc.set_ab(5, 6);
fc.print_ab();
}
}
|
Compliant Solution (final
fields)
If a
and b
have to be kept must remain immutable after their initialization, the simplest approach is to declare them they should be declared as final
. However, this requires the elimination of the setter method set_ab()
.
Code Block | ||
---|---|---|
| ||
class FinalClass { private final int a; private final int b; // ... } |
Compliant Solution (provide copy functionality)
This compliant solution provides a clone()
method in the final
class and does not require the elimination of the setter method. The clone()
method returns a copy of the original object. This new object can be freely used without affecting the original object. Using the clone()
method allows the class to remain mutable. (OBJ10-J. Provide mutable classes with copy functionality to allow passing instances to untrusted code safely)
Code Block | ||
---|---|---|
| ||
final public class NewFinalFinalClass implements Cloneable { private int a; private int b; NewFinalFinalClass(int a, int b){ this.a = a; this.b = b; } void print_ab(){ System.out.println("the value a is: "+ this.a); System.out.println("the value b is: "+ this.b); } void set_ab(int a, int b){ this.a = a; this.b = b; } public NewFinalFinalClass clone() throws CloneNotSupportedException{ NewFinalFinalClass cloned = (NewFinalFinalClass) super.clone(); return cloned; } } public class NewFinalCallerFinalCaller { public static void main(String[] args) throws CloneNotSupportedException { final NewFinalFinalClass nf = new NewFinalFinalClass(1, 2); nf.print_ab(); // Get the copy of original object NewFinalFinalClass nf2 = nf.clone(); // Change the value of a,b of the copy. nf2.set_ab(5, 6); // Original value will not be changed nf.print_ab(); } } |
The clone()
method returns a copy of the original object. This new object can be freely used without exposing the original object. Using the clone()
method allows the class to remain mutable. (OBJ10-J. Provide mutable classes with copy functionality to allow passing instances to untrusted code safely)
The FinalClass
class is declared final
to prevent subclasses from overriding the clone()
method. This enables the class to be accessed and used, while preventing the fields from being modified, and suitably used without any inadvertent modifications of the original object. This compliant solution complies with OBJ10-J. Provide mutable classes with copy functionality to allow passing instances to untrusted code safely.
Noncompliant Code Example (arrays)
Wiki Markup |
---|
AnotherThis commonnoncompliant mistakecode isexample touses use a {{public static final}} array. Clients can trivially modify the contents of the array (although they are unable to change the array itselfreference, as it is {{final}}). In this noncompliant code example, the elements of the {{items\[\]}} array, are modifiable. |
Code Block | ||
---|---|---|
| ||
public static final String[] items = { ... }; |
Compliant Solution
This compliant solution defines a private
array and a public
method that returns a copy of the array.
...
As a result, the original array values cannot be modified by a client.
Compliant Solution (unmodifiable wrappers)
This compliant solution declares a private
array from which a public
immutable list is constructed.
...