1

why we can change a hashMap which is declared as blank final, but we cannot change a primitive type? for example if I create a map

final Map<String, String> someMap;

and initialize it in constructor, and still I can put values in this. But same is not the case with primitive

final int a;

I cant change the value of a in this case. can somebody explain this ?

  • This might be of interest: http://stackoverflow.com/questions/154314/when-should-one-use-final – Qben Aug 29 '13 at 08:59
  • Are you saying you can't change 'a' in the constructor? Because that should be allowed. – Kayaman Aug 29 '13 at 09:01

4 Answers4

3

final means it cannot be changed once initialized. You are just declaring the variable but not initializing it, hence it is allowed.

So doing this is valid

final Map<String, String> someMap;
someMap = new HashMap<String, String>();

But if you try to assign another value to it post initialization then compiler should throw an error that final variable is already intialized:

    final Map<String, String> someMap;
    someMap = new HashMap<String, String>();
    someMap = new TreeMap<String, String>(); //error here

Note: Also putting/removing values in hashmap does not change the reference of the final variable.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • 1
    but same is not allowed in the case of primitives ? –  Aug 29 '13 at 08:55
  • primitives are data types that hold the value, while references are like pointers, which do not hold their values but point to their values and are used on/with objects. If you understand this part, then you will understand also why you can do put on a final hashMap reference. – Juned Ahsan Aug 29 '13 at 09:08
1

It is just the reference to the map (i.e. the variable someMap) which cannot be changed. The map itself can be changed. You can for example insert values. But you cannot assign a new map to someMap.

Henry
  • 42,982
  • 7
  • 68
  • 84
1

When using the final keyword on variables you are saying that the variable can be defined only once. In other words once a value has been assigned to the variable, it cannot be reassigned.

This yields obvious behavior with primitive types but is less obvious with objects. Importantly though when inserting values into a map, the object instance remains the same. This is important to remember when passing objects to methods, and really important when using get/set/clone methods as you may end up with multiple references to the same object, where a change in one place (insert entry into map) may have undefined effects in others.

If the Map in your question is important you can use java.util.Collections.unmodifiableMap(m); to stop people fiddling with it.

JohnMark13
  • 3,709
  • 1
  • 15
  • 26
1

*emphasized text*When you write:

final Map<String, String> someMap;

it's important to realise that someMap is a reference, and you're declaring the reference to be final. The actual object is not immutable, but the reference is. Hence you can't change the reference i.e. you can't do:

someMap = anotherMap;

later on.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440