...
At times, a class is labeled final
with no accessible copy methods. Callers can then obtain an instance of the class, create a new instance with the original state and subsequently proceed to use it. Similarly, mutable objects obtained must also be copied when necessary.
Wiki Markup |
---|
\[[SCG 07|AA. Java References#SCG 07]\] provides some very useful tips: |
If a class is final and does not provide an accessible method for acquiring a copy of it, callers can resort to performing a manual copy. This involves retrieving state from an instance of that class, and then creating a new instance with the retrieved state. Mutable state retrieved during this process must likewise be copied if necessary. Performing such a manual copy can be fragile. If the class evolves to include additional state in the future, then manual copies may not include that state.
Risk Assessment
Creating a mutable class without a clone
method may result in the data of the class becoming corrupted.
...