Collections.unmodifiableList(...)
returns a new instance of a static inner class UnmodifiableList
. Other unmodifiable collections classes are constructed same way.
Were these classes public, one had two advantages:
UnmodifiableList
), so an API user wouldn't come to the idea of modifying that collection;List
is instanceof UnmodifiableList
.So, were there any advantages not to make those classes public?
EDIT: No definitely convincing arguments were presented, so I choose the most upvoted answer.
I think both advantages are there but are not that useful. The main problems remain the same: UnmodifiableList still is a List and thus all the setters are available and the underlying collections still are modifiable. Making the class UnmodifiableList public would add to the illusion of being unmodifiable.
The nicer way would be for the compiler to help, but for that the collection class hierarchies would have to changed a lot. E.g., the collection API of Scala is way more advanced in that respect.
A disadvantage would be the introduction of at least three additional classes / interfaces into the API. Because of them not being that useful, I think leaving them out of the API is a good choice.