此類帶有來自 JCIP 注釋包的“不可變”注釋。然而,該類的可變字段之一具有在構造函數和析構函數之外對其進行調用的變化的方法。
例 1:不可變最終類的下列代碼錯誤地將集聲明為 public 和 final。
@Immutable
public final class ThreeStooges {
private final Set stooges = new HashSet>();
...
public void addStooge(String name) {
stooges.add(name);
}
}
[1] Standards Mapping - Common Weakness Enumeration - (CWE) CWE ID 471
[2] B. Goetz Java Concurrency in Practice.Chapter 3:Sharing Objects Guidelines
[3] Package net.jcip.annotations Specification