can someone explain me this:
final AtomicReference<Integer> atomicReference = new AtomicReference<>(1);
atomicReference.set(2);
In what sense is final used?
can someone explain me this:
final AtomicReference<Integer> atomicReference = new AtomicReference<>(1);
atomicReference.set(2);
In what sense is final used?
In what sense is final used?
The variable itself is final. You can't change the variable's value to refer to a different AtomicReference
object.
Calling set
on the object and thus changing the data within the object isn't the same thing at all.
To put it in more real world terms, I can give you my home address and say, "You can't change where I live." That doesn't stop you from painting my front door green though (i.e. making a change to the house that the address refers to.)
final
prevents you from changing the variable to refer to a different instance.
It does not prevent you from mutating the existing instance.
It means that you can't write
atomicReference = something;
final
means that atomicReference
can not reference another AtomicReference
anymore.
final
just means that the variable/object cannot be reassign.
But you can modify your object through setter for example
Check it out: