what is the sense of final ArrayList?
But what is effect making it's final?
This means that you cannot rebind the variable to point to a different collection instance:
final List<Integer> list = new ArrayList<Integer>();
list = new ArrayList<Integer>(); // Since `list' is final, this won't compile
As a matter of style, I declare most references that I don't intend to change as final
.
I still can add to ArrayList new elements, remove elements and update it.
If you wish, you can prevent insertion, removal etc by using Collections.unmodifiableList()
:
final List<Integer> list = Collections.unmodifiableList(new ArrayList<Integer>(...));
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
If you really want an immutable list, you should use the Collections.unmodifiableList()
method.