0

Which advantages/disadvantages of making ArrayList (or other Collection) final? And what if we try to do so like:

final List<Integer> A=new ArrayList<Integer>(1,2,3,4,5);
List<Integer> l=new ArrayList<Integer>(4,8,16);
A=l;

Is this is valid? Now the reference A will point to this Array list pointed by l? Please help.

4
  • 3
    1) final affects the variable, and not the object to which is refers. 2) It is not not allowed to re-assign to a final variable. The compiler should have said as much. Commented Jan 8, 2018 at 21:04
  • 1
    stackoverflow.com/a/10380512/2864740 (this answer is more generalized than the question) , stackoverflow.com/questions/15655012 (specific to final fields, discusses object mutability as well) Commented Jan 8, 2018 at 21:07
  • 1
    Also, ArrayList does not have a constructor that will allow that usage. Commented Jan 8, 2018 at 21:09
  • ok thanks a lot for help guys :) Commented Jul 21, 2018 at 8:44

1 Answer 1

1

No, reassignment to final reference is not allowed in Java.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.