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 the variable is a reference to an object, however, what appears to be final may not always be. Consider for example, a final
method parameter that is a references 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 \[[JLS 05|AA. Java References#JLS 05]\]: |
... 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
...
References
Wiki Markup |
---|
\[[JLS 0605|AA. Java References#JLS 0605]\] Section 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" |
...