What does it mean for a collection to be declared final in Java? Is it that no more elements can be added to it? Is it that the elements already there cannot be changed? Is it something else?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Making the variable final makes sure you cannot re-assign that object reference after it is assigned. If you combine the final keyword with the use of Collections.unmodifiableList, you get the behaviour that you describe:
This has as result that the list pointed to by fixedList cannot be changed. it can still be change through the someList reference (so make sure it is out of scope after this asignment.)
More simple example is taking a rainbow class adding colors of rainbow in a hashset
You can't do this,the reference is
FINAL
JLS 4.12.4
No. It simply means that the reference cannot be changed.
You are getting confused between final and immutable Objects.
final
--> You cannot change the reference to the collection (Object). You can modify the collection / Object the reference points to. You can still add elements to the collectionimmutable
--> You cannot modify the contents of the Collection / Object the reference points to. You cannot add elements to the collection.