Saturday, June 16, 2018

What do you mean by final ArrayList?

Problem statement: What is the sense of final ArrayList?

It just mean that you cannot re-assign the reference to new collection object reference. Attempting to do it will lead compile time error. 
e.g. 
[1] - 
final List<String> list = new ArrayList<String>();
list = new ArrayList<String>(); // Since `list' is final, this won't compile
^ 
compile time error here
[2] -
final List<String> linked = new LinkedList<String>();
linked = new LinkedList<String>(); // Since `linked' is final, this won't compile

compile time error here

Note: But I still can add to ArrayList new elements, remove elements and update it.

No comments:

Post a Comment

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...