...
Noncompliant Code Example
Suppose that SomeType
is immutable.
Code Block | ||
---|---|---|
| ||
public static final SomeType [] SOMETHINGS = { ... }; |
With this This declaration , SOMETHINGS[1]
, etc. can allows the SOMETHINGS
array to be modified by untrusted clients of the code. Any element of the array can be assigned a new value.
This noncompliant code example also violates OBJ01-J. Limit accessibility of fields.
...
Code Block | ||
---|---|---|
| ||
private static final SomeType [] SOMETHINGS = { ... }; public static final SomeType [] somethings() { return SOMETHINGS.clone(); } |
Now, the original array values cannot be modified by a any client.
Compliant Solution 2
...