Wiki Markup |
---|
Programmers sometimes incorrectly believe that declaring a field or variable {{final}} makes the referenced object immutable. Declaring variables that have a primitive type to be {{final}} does prevent changes to their values after initialization (other than through the use of the unsupported {{sun.misc.Unsafe}} class). However, when the variable has a reference type, the presence of a {{final}} clause in the declaration only makes _the reference itself_ immutable. The {{final}} clause has no effect on the referenced object. Consequently, the fields of the referenced object can be mutable. For example, according to the Java Language Specification \[[JLS 2005|AA. Bibliography#JLS 05]\], [Section 4.12.4|http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.12.4], "{{final}} Variables": |
...
OBJ00-J. Declare data members as private and provide accessible wrapper methods 04. Object Orientation (OBJ) OBJ02-J. Sensitive classes must not let themselves be copied