...
Wiki Markup |
---|
At times, a mutable class's member class is declared {{final}} with no accessible copy methods. Callers can use the {{clone()}} method to obtain an instance of the mutable class. This {{clone()}} method must create a new instance of the {{final}} member class and copy the original state to it. The new instance is necessary because no accessible copy method may be available in the member class. If the member class evolves in the future, it is critical to include the new state in the manual copy. \[[SCG 07|AA. Java References#SCG 07]\] |
As far as possible, a mutable class that defines a clone()
method, must be declared final
. This ensures that untrusted code cannot subclass and override the clone()
method to supply a spurious instance. Also refer to FIO31-J. Defensively copy mutable inputs and mutable internal components.
Exceptions
EX1: Sensitive classes should not be made cloneable. (MSC37-J. Make sensitive classes noncloneable)
...