I have final reference to the arraylist and I have learned that ArrayList internally copies the array contents to new location once it runs out of initial size and then adds new element to the new copied array then the reference points to new location. Then how can final reference variable points to new location of copied array?
-
post the code.....the fact that the ArrayList has final reference has nothing to do with the objects the list is holding – ΦXocę 웃 Пepeúpa ツ Jun 23 '17 at 11:38
-
You have a final reference that points to an ArrayList and that ArrayList has a non final reference to an array where it keeps its data. They have nothing to do with each other. – OH GOD SPIDERS Jun 23 '17 at 11:39
-
It's not about code, Internally how can final variable points to new location when one adds 11th element to ArrayList. asking about the internal working. – Rohit Haval Jun 23 '17 at 11:42
-
it doesn't point to a new location. it doesn't need to. See my previous comment. And if you want to know the internal working you can just look up the source code of ArrayList. – OH GOD SPIDERS Jun 23 '17 at 11:46
1 Answers
final' simply makes the object reference unchangeable( that is, the reference it has can't be substituted with a new one). The object it points to is not immutable by doing this. if the attribute is self is modifiable it is ok to do what you have just described.
In other word : Once a final variable has been assigned, it always contains the same value. If a final variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object. This applies also to arrays, because arrays are objects; if a final variable holds a reference to an array, then the components of the array may be changed by operations on the array, but the variable will always refer to the same array. So In case of ArrayList internal Array is an instance variable and you can modify it.

- 2,576
- 2
- 10
- 16