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?
“final double java” Code Answer you can add or remove elements from final array or final collection. 10. It is good practice to represent final variables in all uppercase, using. 11. underscore to separate words.
You use the final keyword in a method declaration to indicate that the method cannot be overridden by subclasses. The Object class does this—a number of its methods are final .
It just means that you can't re-assign its reference. Attempting to do something like the below will lead to compiler error. final List<String> list = new ArrayList<String>(); list = new LinkedList<String>(); ^ Compiler error here.
A final class is a class that can't be extended. Also methods could be declared as final to indicate that cannot be overridden by subclasses. Preventing the class from being subclassed could be particularly useful if you write APIs or libraries and want to avoid being extended to alter base behaviour.
No. It simply means that the reference cannot be changed.
final List list = new LinkedList();
....
list.add(someObject); //okay
list.remove(someObject); //okay
list = new LinkedList(); //not okay
list = refToSomeOtherList; //not okay
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 collection
immutable
--> You cannot modify the contents of the Collection / Object the reference points to. You cannot add elements to the collection.
You can't do this,the reference is FINAL
final ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> list2 = new ArrayList<Integer>();
list=list2;//ERROR
list = new ArrayList<Integer>();//ERROR
JLS 4.12.4
Once a final variable has been assigned, it always contains the same value. If a final variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object.
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:
final List fixedList = Collections.unmodifiableList(someList);
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
public static class Rainbow {
/** The valid colors of the rainbow. */
public static final Set VALID_COLORS;
static {
Set temp = new HashSet();
temp.add(Color.red);
temp.add(Color.orange);
temp.add(Color.yellow);
temp.add(Color.green);
temp.add(Color.blue);
temp.add(Color.decode("#4B0082")); // indigo
temp.add(Color.decode("#8A2BE2")); // violet
VALID_COLORS = Collections.unmodifiableSet(temp);
}
/**
* Some demo method.
*/
public static final void someMethod() {
Set colors = RainbowBetter.VALID_COLORS;
colors.add(Color.black); // <= exception here
System.out.println(colors);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With