File: MutablePublicArray.md

package info (click to toggle)
error-prone-java 2.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 23,204 kB
  • sloc: java: 222,992; xml: 1,319; sh: 25; makefile: 7
file content (14 lines) | stat: -rw-r--r-- 576 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Nonzero-length arrays are mutable. Declaring one `public static final` indicates
that the developer expects it to be a constant, which is not the case. Making it
`public` is especially dangerous since clients of this code can modify the
contents of the array.

There are two ways to fix this problem:

1.  Refactor the array to an `ImmutableList`.
2.  Make the array `private` and add a `public` method that returns a copy of
    the `private` array.

See [Effective Java 3rd Edition ยง15][ej3e-15] for more details.

[ej3e-15]: https://books.google.com/books?id=BIpDDwAAQBAJ