4

can someone explain me this:

final AtomicReference<Integer> atomicReference = new AtomicReference<>(1);
atomicReference.set(2);

In what sense is final used?

GedankenNebel
  • 2,437
  • 5
  • 29
  • 39

4 Answers4

15

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.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

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;
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

final means that atomicReference can not reference another AtomicReference anymore.

Cratylus
  • 52,998
  • 69
  • 209
  • 339
1

final just means that the variable/object cannot be reassign.

But you can modify your object through setter for example

Check it out:

http://en.wikipedia.org/wiki/Final_(Java)

TheEwook
  • 11,037
  • 6
  • 36
  • 55