If the instance variable is not immutable, the state of this variable can be changed, even if it is final. Take for example:
private final List<Foo> foos = new ArrayList<Foo>(); public void addFoo(Foo newFoo){ foos.add(newFoo); } public Foo popFoo(){ return foos.remove(0); }
In this situation, even if foos is final, a Foo can be added to Thread B, while Thread A tries to remove the element, which leads to a potentially compatible state.
The tutorial on synchronized methods, mentioned in other examples, is true that reading the final variable does not need to be synchronized. However, if the variable is changed (as List<T> is), then the record in this variable must be synchronized to guarantee the connection "will happen sooner". If, however, the variable is unchanged, then, of course, records are still not allowed on this variable, so there is no need to synchronize.
Scott Fines Feb 03 2018-10-15T00 : 00Z
source share