Versions Compared

Key

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

When Sometimes, when a variable is declared final, it is believed to be immutable. If the variable is a primitive type, it can undoubtedly be made final. If declaring it final means that its value cannot be subsequently changed. However, if the variable is a reference to an a mutable object, however, what the object's contained data that appears to be final immutable may not always actually be mutable. Consider for example, a final method parameter that is a reference to an object. The argument to this method will use pass-by-value to copy the reference but the referenced data will remain 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.

...

In this example, the values of instance fields a and b have been changed can be changed even after their initialization. When a an object reference is declared final, it only signifies that the reference cannot be changed, while whereas the contents that it refers to can still be.

Code Block
bgColor#ffcccc
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);
  }
  private int a;
  private int b;
}

public class FinalCaller {
  public static void main(String[] args) {
    final FinalClass fc = new FinalClass(1,2);
    fc.print_ab();
    //now we change the value of a,b.
    fc.set_ab(5, 6);
    fc.print_ab();
  }
}

...

If a and b have to be kept immutable after their initialization, the simplest approach is to declare them as final.

Code Block
bgColor#ffcccc
private final int a;
private final int b;

void set_ab(int a, int b){ //But now the compiler complains about set_ab method!
  this.a = a;
  this.b = b;
 }
 private final int a;
 private final int b;

Unfortunately, now one cannot have setter methods of a and b.

...

An alternative approach is to provide a clone method in the class. The clone method can be used to get a copy of the original object. Now one can do anything to this 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;
 }
 private int a;
 private int b; }
}

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();
           //now we change the value of a,b of the copy.
          nf2.set_ab(5, 6);
	      //but the original value will not be changed
	      nf.print_ab();
    } catch (CloneNotSupportedException e) { e.printStackTrace();/* Forward to handler */ }
  }
}

The class is made 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.

...

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 to change the array itself, as it is final).

...

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

Code Block
bgColor#ccccff
private static final SomeType [] SOMETHINGS = { ... };
public static final SomeType [] somethings() {
  return SOMETHINGS.clone();
}

...

An alternative approach is to have a private array from which a public immutable list is constructed:

...

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

...

Using final to declare the reference to an a mutable object is a potential security risk because the contents of the object can still be changed.

...