Why make private static final Lists/Sets/Maps unmodifiable?

If you accidentally return PREFIXES from a method, suddenly any other code out there can modify it. Making constants truly immutable defends against your own stupidity when you modify that code in the future at 3AM.


By making the list unmodifiable the author documented his assumption that the values will never change. Whoever might edit that class later on can not only see that assumption, but will also be reminded in case it is ever broken.

This makes sense only when taking the longer-term view. It reduces the risk of new problems arising through maintenance. I like to do this style of programming, because I tend to break stuff even in my own classes. One day you might go in for a quick fix and you forget about an assumption that was made originally and is relevant for correctness. The more you can lock the code down, the better.


It's surprisingly easy to have a private map, collection or array which is modifiable from outside the class. You'd mark it final, why wouldn't also spell out that it is supposed to be immutable as well?