3 ms·
Unfortunately unlike C++ etc constness doesn't propagate inside objects marked final. So final List x = new ArrayList() stops x being reassigned but does
by zmmmmm 8y ago
Unfortunately unlike C++ etc constness doesn't propagate inside objects marked final. So
final List x = new ArrayList()
stops x being reassigned but does nothing to stop the downstream code from mutating the contents of the list.
- xxs 8y agofinal List<?> immutable = Collections.immutableList(x)... and pass it around. Never expose your collections directly (unless you are willing to go copy on write and immutable objects inside the collections, the latter is very welcome, though)
- stickfigure 8y agoAlso, if you want to go 'whole hog' you can use any of the functional/immutable collection libraries like vavr, functionaljava, or jimmutable.