Versions Compared

Key

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

...

Wiki Markup
According to the Java Language Specification \[[JLS 2005|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 (

...

Mutable Class, final

...

Reference)

In this noncompliant code example, the values of instance fields x and y can be changed even after their initialization despite the point instance being declared as final.

...

When an object reference is declared final, it only signifies that the reference cannot be changed, whereas the referenced contents can still be altered.

Compliant Solution (final

...

Fields)

If x and y must remain immutable after their initialization, they should be declared as final. However, this requires the elimination of the setter method set_xy().

Code Block
bgColor#ccccff
class Point {
  private final int x;
  private final int y;

  Point(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);
  }
}

Compliant Solution (

...

Provide Copy Functionality)

This compliant solution provides a clone() method in the final class Point and does not require the elimination of the setter method.

...

The clone() method returns a copy of the original object and reflects its latest state. This new object can be freely used without exposing the original object. Using the clone() method allows the class to remain mutable. (See guideline OBJ10-J. Provide mutable classes with copy functionality to allow passing instances to untrusted code safely.)

The Point class is declared final to prevent subclasses from overriding the clone() method. This enables the class to be suitably used without any inadvertent modifications of the original object. This compliant solution complies with guideline OBJ10-J. Provide mutable classes with copy functionality to allow passing instances to untrusted code safely.

Noncompliant Code Example (

...

Arrays)

This noncompliant code example uses a public static final array, items.

...

Clients can trivially modify the contents of the array, though they are unable to change the array reference because it is final.

Compliant Solution (

...

Clone the

...

Array)

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. Note that sometimes, a manual deep copy may be required when dealing with arrays of objects. This generally happens when the objects do not export a clone() method. Refer to guideline FIO00-J. Defensively copy mutable inputs and mutable internal components for more information.

Compliant Solution (

...

Unmodifiable Wrappers)

This compliant solution declares a private array from which a public immutable list is constructed.

...

Using final to declare the reference to a mutable object is potentially misleading because the contents of the object can still be changed.

Recommendation Guideline

Severity

Likelihood

Remediation Cost

Priority

Level

OBJ01-J

low

probable

medium

P4

L3

...