Versions Compared

Key

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

...

... 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 example, the values of a and b have been changed. When a reference is declared final, it only signifies that the reference cannot be changed, while the contents that it refers to can still be.

Code Block
bgColor#ffcccc
class FinalClass{
  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();
  }
}

Noncompliant Code Example

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

...

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

Compliant Solution

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 new object without affecting the original object.

...

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.

Noncompliant Code Example

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

...

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

Compliant Solution

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:

...

Now the original array values cannot be modified by a client.

Compliant Solution

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.

Risk Assessment

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

Recommendation

Severity

Likelihood

Remediation Cost

Priority

Level

OBJ03-J

low

probable

medium

P4

L3

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

References

Wiki Markup
\[[JLS 05|AA. Java References#JLS 05]\] Sections 4.12.4 "final Variables" and 6.6, "Access Control"
\[[Bloch 08|AA. Java References#Bloch 08]\] Item 13: Minimize the accessibility of classes and members
\[[Core Java 04|AA. Java References#Core Java 04]\] Chapter 6
\[[MITRE 09|AA. Java References#MITRE 09]\] [CWE ID 607|http://cwe.mitre.org/data/definitions/607.html] "Public Static Final Field References Mutable Object"

...