When developing a small API, I was going to write a static value that refers to an array of String: public static final String[] KEYS={"a","b","c"} I found that this is indicated as a "security hole" in the article 14 "Effective Java" by Joshua Bloch, where he offers an alternative by declaring an array of te 'private' and providing a public receiver that returns an unmodifiable list:
return Collections.unmodifiableList(Arrays.asList(KEYS))
I just canβt understand why this is necessary, the array in the original expression is declared final, even if its publication and its elements are immutable, how can this be changed from the outside of the code?
source share