0

I have been doing some tutorial videos on Youtube, and one thing that got my interest is that after declaring an immutable class variable we can still change in the methods.

private final static List<Object> someList = new ArrayList<>();

And after I have instantiated this "someList" variable, I can still add some data to it. Can someone explain why can we do it, since we declared it final it should be immutable

alex_z
  • 416
  • 5
  • 12
  • 3
    You are mixing up final with immutable. They are different. Final = Variable cannot be reassigned to something else, immutable = Object cannot be changed. If you want an Immutable List you can use `Arrays.asList();` for example. – OH GOD SPIDERS Mar 21 '18 at 13:55
  • My bad. I am still learning Java and maybe don’t know all the differences – alex_z Mar 21 '18 at 13:57
  • Thank you Emre Acar, that answered my question – alex_z Mar 21 '18 at 13:59

0 Answers0