Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the sense of final ArrayList?

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 is effect making it's final?

like image 913
WelcomeTo Avatar asked May 25 '12 08:05

WelcomeTo


People also ask

What is the use of final List in Java?

Output Explanation: The array arr is declared as final, but the elements of an array are changed without any problem. Arrays are objects and object variables are always references in Java. So, when we declare an object variable as final, it means that the variable cannot be changed to refer to anything else.

What happens if we make List as final in Java?

A List with final keyword, still you can add as well as remove. But you cannot copy the list into another list.

How we can use final arrays in Java?

A final variable can be explicitly initialized only once. A reference variable declared final can never be reassigned to refer to a different object. However, the data within the object can be changed.

Can final List be modified?

In Java, we know that String objects are immutable means we can't change anything to the existing String objects. final means that you can't change the object's reference to point to another reference or another object, but you can still mutate its state (using setter methods e.g).


4 Answers

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>(...));
like image 163
NPE Avatar answered Oct 19 '22 20:10

NPE


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.

like image 17
adarshr Avatar answered Oct 19 '22 19:10

adarshr


You won't be able to modify its reference using new ArrayList for example.

like image 8
Michael Laffargue Avatar answered Oct 19 '22 18:10

Michael Laffargue


Making the variable final makes sure you cannot re-assign that objest reference after it is assigned. As you mention you can still use that lists methods to make changes.

If you combine the final keyword with the use of Collections.unmodifiableList, you ge the behaviour you are probably trying to achieve, for instance:

final List fixedList = Collections.unmodifiableList(someList);

This has as result that the list pointed to by fixedList cannot be changed. Beware however that it can still be change through the someList reference (so make sure it is out of scope after this asignment.)

like image 5
rsp Avatar answered Oct 19 '22 20:10

rsp