Which advantages/disadvantages we can get by making ArrayList (or other Collection) final? I still can add to ArrayList new elements, remove elements and update it. But what
I came to think of this same question and coded and example to add to the explanation from yet another angle.
The final arrayList can still be modified, refer to the example below and run it to see for your self.
Here is the immutable class with immutable List declaration:
public final class ImmutableClassWithArrayList {
final List theFinalListVar = new ArrayList();
}
And here is the driver:
public class ImmutableClassWithArrayListTester {
public static void main(String[] args) {
ImmutableClassWithArrayList immClass = new ImmutableClassWithArrayList();
immClass.theFinalListVar.add("name");
immClass.theFinalListVar.forEach(str -> System.out.println(str));
}
}
As you can see, the main method is adding (modifying) the list. So the only thing to note is that the "reference" to the object of the collection type can't be re-assigned to another such object. As in the answer by adarshr above, you can't do immClass.theFinalListVar = new ArrayList(); in the main method here.
The modification part really helped me understand this and hope it helps in same way.