What is the meaning of the final ArrayList?

What advantages / disadvantages can we get by making the final release of ArrayList (or another collection)? I can still add new elements to ArrayList, delete elements and update them. But what is the effect that makes this final?

+51
java arraylist final
May 25 '12 at 8:08
source share
11 answers

But what is the effect that makes it final?

This means that you cannot reset the variable to point to another instance of the collection:

final List<Integer> list = new ArrayList<Integer>(); list = new ArrayList<Integer>(); // Since `list' is final, this won't compile 

As a style, I declare most of the links that I do not intend to modify as final .

I can still add new elements to ArrayList, delete elements and update them.

If you want, you can prevent installation, uninstallation, etc. using Collections.unmodifiableList() :

 final List<Integer> list = Collections.unmodifiableList(new ArrayList<Integer>(...)); 
+104
May 25 '12 at 8:10
source share

It just means that you cannot reassign your link. Attempting to do something like the following will result in a compiler error.

 final List<String> list = new ArrayList<String>(); list = new LinkedList<String>(); ^ Compiler error here 

If you really need an immutable list, you should use the Collections.unmodifiableList() method.

+13
May 25 '12 at 8:10
source share

You cannot change your link using new ArrayList , for example.

+7
May 25 '12 at 8:11
source share

Executing the final variable ensures that you cannot reassign this link to execute after it is assigned. As you mentioned, you can still use these lists to make changes.

If you combine the final keyword with Collections.unmodifiableList , you can define the behavior that you are probably trying to achieve, for example:

 final List fixedList = Collections.unmodifiableList(someList); 

This leads to the fact that the list pointed to by fixedList cannot be changed. Beware, however, that this can still be changed using the someList link (so make sure that after this binding it goes out of scope.)

+5
May 25 '12 at 8:19
source share

This does not affect what you can do with the ArrayList on your own - the ArrayList itself is still modified. You just made the link unchanged.

But creating a final variable has other advantages:

  • This prevents the variable from changing if it remains constant. This can help prevent future errors.
  • Creating final variables can help the compiler perform certain performance optimizations.

In general, the more things you do unchanged, the better. Therefore, ending links (even if they are references to mutable objects) is usually a good idea.

+3
May 25 '12 at 8:15
source share

final has many consequences with multithreading.

  • JMM clearly defines the finalization of the final field.

What is not clearly defined:

  • Compilers are free to reorder them across memory barriers.
  • Compilers can always read a cached copy.
+3
Aug 27 '16 at 23:01
source share

A final is a keyword or reserved word in java and can be applied to member variables, methods, class, and local variables in Java. Once you make a control ending, you are not allowed to change this link and the compiler will check this and raise a compilation error if you try to reinitialize the final variables in java.

+1
May 25 '12 at 8:13
source share

You cannot reinstall it into another collection, as aix said.

Example: Combined with the unaltered implementation of the list, you get safe members that you can make public.

Example: When you rely on this link, it does not change, you need a final. This is true, for example, in synchronization scenarios.

There may be many more examples. This is a good style to declare members final, if you are not going to change the link at all.

+1
May 25 '12 at 8:17
source share

I personally mark the collection field of my classes as final to save users of my class from checking if it is null or not. This works because once a value has already been assigned to a destination variable, it can never be reassigned to another value, including null.

+1
Jun 15 '16 at 2:22
source share

To get a truly immutable list, you will need to make deep copies of the contents of the list. UnmodifiableList will only display the list of links somewhat unchanged. Now creating a deep copy of a list or array will be hard on memory with increasing size. You can use serialization / deserialization and store a deep copy of the array / list in a temporary file. The installer will not be available, since the varaible element must be immutable. The getter will serialize the member variable to a file and then desalize it to get a deep copy. Seralization has an innate nature of penetration into the depths of the tree of objects. This will ensure complete constancy at some operating costs.

  package com.home.immutable.serial; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.List; public final class ImmutableBySerial { private final int num; private final String str; private final ArrayList<TestObjSerial> immutableList; ImmutableBySerial(int num, String str, ArrayList<TestObjSerial> list){ this.num = num; this.str = str; this.immutableList = getDeepCloned(list); } public int getNum(){ return num; } public String getStr(){ return str; } public ArrayList<TestObjSerial> getImmutableList(){ return getDeepCloned(immutableList); } private ArrayList<TestObjSerial> getDeepCloned(ArrayList<TestObjSerial> list){ FileOutputStream fos = null; ObjectOutputStream oos = null; FileInputStream fis = null; ObjectInputStream ois = null; ArrayList<TestObjSerial> clonedObj = null; try { fos = new FileOutputStream(new File("temp")); oos = new ObjectOutputStream(fos); oos.writeObject(list); fis = new FileInputStream(new File("temp")); ois = new ObjectInputStream(fis); clonedObj = (ArrayList<TestObjSerial>)ois.readObject(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { try { oos.close(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } return clonedObj; } } 
+1
May 27 '17 at 7:13
source share

Essentially, you are trying to reach here to make the list immutable, I think. However, when you mark the final list of links, this means that the link can not be pointed to any other list object other than this.

If you want the final (immutable) ArrayList to go instead to the utility of the Collections.unmodifaibleList (list) class.

0
Aug 6 '17 at 12:51 on
source share



All Articles