Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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 subsequently changed. However, if the variable is a reference to a mutable object, the object's contained data that appears to be immutable, may actually be mutable. Consider for example, a final method parameter that is a reference to an object. The argument to this method will use uses pass-by-value to copy the reference but the referenced data will remain remains mutable.

Wiki Markup
According to the Java Language Specification \[[JLS 05|AA. Java References#JLS 05]\], section 4.12.4 "{{final}} Variables":

... 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

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 contents that it refers to can still be.

...

The problem with this method is that setter methods cannot be used to alter a and b.

Compliant Solution

An alternative approach is to provide This compliant solution provides a clone() method in the class. The clone() method can be used to get a copy of the original object. This new object can be freely used without affecting the original object.

Code Block
bgColor#ccccff
final public class NewFinal implements Cloneable {
  private int a;
  private int b;

  NewFinal(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 NewFinal clone() throws CloneNotSupportedException{
    NewFinal cloned = (NewFinal) super.clone();
    return cloned;
  }
}

public class NewFinalCaller {
  public static void main(String[] args) {
    final NewFinal nf = new NewFinal(1,2);
    nf.print_ab();
    //get the copy of original object
    try {
      NewFinal 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();
    } catch (CloneNotSupportedException e) { /* Forward to handler */ }
  }
}

The class is made 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 complies with OBJ36-J. Provide mutable classes with a clone method to allow passing instances to untrusted code safely.

...

Another common mistake is to use a public static final array. Clients can trivially modify the contents of the array (although they will not be able are unable to change the array itself, as it is final).

Wiki Markup
With this declaration, {{SOMETHINGS\[1\]}}, etc. can be modified by clients of the code.

Code Block
bgColor#ffcccc
public static final SomeType [] SOMETHINGS = { ... };

Compliant Solution

One approach is to make use of the above method: first define This compliant solution first defines a private array and then provide provides a public method that returns a copy of the array.

...

Code Block
bgColor#ccccff
private static final SomeType [] THE_THINGS = { ... };
public static final List<SomeType> SOMETHINGS =
Collections.unmodifiableList(Arrays.asList(THE_THINGS));

Now, neither the original array values nor the public list can be modified by any client.

...