Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Final arraylist

Tags:

java

My question is regarding declaring an arraylist as final. I know that once I write final ArrayList list = new ArrayList(); I can add, delete objects from this list, but I can not list = new ArrayList() or list = list1. But what will be the use of declaring arraylist as Private static final ArrayList list = new ArrayList();. And apart from the difference I have mentioned above what will be the difference between following two declaration:

1. ArrayList list = new ArrayList()

2. private static final ArrayList list =  new ArrayList();
like image 624
user553198 Avatar asked Dec 24 '10 10:12

user553198


People also ask

What is final ArrayList in Java?

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 reference final you are not allowed to change that reference and compiler will verify this and raise compilation error if you try to re-initialized final variables in java.

How do you declare a final ArrayList in Java?

public final ArrayList anotherArrayList = new ArrayList(); anotherArrayList. addAll(someArrayList); I had hoped to store this "anotherArrayList" as a global ArrayList object and use it, but this returns a nullpointer exception.

Can we use final in ArrayList?

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.

Can a List be final in Java?

No, the final keyword does not make the list, or its contents immutable. If you want an immutable List, you should use: List<Synapse> unmodifiableList = Collections.


3 Answers

Just to "bring a little water to your Mill" you will understand the interest of final when you'll want to make your list publically availiable but unmodifiable.

In java one can make a list unmodifiable with Collections.unmodifiableList(modifiableList).

Now have a look to the following code :

public class MyClass{
  public static List<String> MY_PUBLIC_LIST;
  static{
    ArrayList<String> tmp = new ArrayList<String>();
    tmp.add("a");
    tmp.add("b");
    tmp.add("c");
    MY_PUBLIC_LIST = tmp;
  }
}

Well, in anyclass, anywhere in your code you can do something like this

MyClass.MY_PUBLIC_LIST = null;
MyClass.MY_PUBLIC_LIST = new ArrayList<String>();
MyClass.MY_PUBLIC_LIST.clear();
MyClass.MY_PUBLIC_LIST.add("1");

When you add the final keyword to your variable, the first two won't be allowed

public static final List<String> MY_PUBLIC_LIST;

But you'll still be able to modify the content of the list :

MyClass.MY_PUBLIC_LIST.clear();
MyClass.MY_PUBLIC_LIST.add("1");

By adding a Collections.unmodifiableList(modifiableList) at the end of the static block you'll prevent this too :

MY_PUBLIC_LIST = Collections.unmodifiableList(tmp);

Ok we are almost there. Just to be sure you get the whole picture lets keep the Collections.unmodifiableList(modifiableList) but let me remove the final modifier

public class MyClass{
  public static List<String> MY_PUBLIC_LIST;
  static{
    ArrayList<String> tmp = new ArrayList<String>();
    tmp.add("a");
    tmp.add("b");
    tmp.add("c");
    MY_PUBLIC_LIST = Collections.unmodifiableList(tmp);
  }
}

What can you do in that case ?

...

...

Well you can do whatever you want like in the first case (given that you assign the new list first) :

MyClass.MY_PUBLIC_LIST = null;
MyClass.MY_PUBLIC_LIST = new ArrayList<String>();
MyClass.MY_PUBLIC_LIST.clear();
MyClass.MY_PUBLIC_LIST.add("1");
like image 77
Cerber Avatar answered Oct 04 '22 23:10

Cerber


You're right that declaring the list final means that you cannot reassign the list variable to another object.

The other question (I think) was

public class SomeClass {
    private static final ArrayList list = new ArrayList();
}

vs

public class SomeClass {
    ArrayList list = new ArrayList();
}

let's take each modifier in turn.

private Means only this class (SomeClass) can access list

static Means that there is only one instance of the list variable for all instances of SomeClass to share. The list instance is associated with the SomeClass class rather than each new SomeClass instance. If a variable is non-static it's said to be an instance variable

final as you know means that you cannot reassign the list variable another value.

In the second declaration there are no modifiers, so the variable is an instance variable and it also gets package-private access protection (Sometimes called default access protection). This means that this class (SomeClass) and other classes in the same package can access the variable.

You can find out more about public, private, and package-private here: Access control

You can find out more about final and static here: Class variables

like image 35
daveb Avatar answered Oct 05 '22 01:10

daveb


When you say

final ArrayList list = new ArrayList();

this means that the variable list will always point to the same ArrayList object. There are two situations in which this can be useful.

  1. You want to make sure that no-one reassigns your list variable once it has received its value. This can reduce complexity and helps in understanding the semantics of your class/method. In this case you are usually better off by using good naming conventions and reducing method length (the class/method is already too complex to be easily understood).
  2. When using inner classes you need to declare variables as final in an enclosing scope so that you can access them in the inner class. This way, Java can copy your final variable into the inner class object (it will never change its value) and the inner class object does not need to worry what happens to the outer class object while the inner class object is alive and needs to access the value of that variable.

The second part of your question is about the difference between

ArrayList list = new ArrayList();

and

private static final ArrayList list = new ArrayList();

The difference of course are the modifiers. private means not visible outside the class, static means that it is defined on the class level and doesn't need an instance to exist, and final is discussed above. No modifiers means package-private or default access.

like image 31
eljenso Avatar answered Oct 05 '22 00:10

eljenso