Why final instance class variable in Java?

后端 未结 11 738
南方客
南方客 2020-12-09 05:13

If instance variable is set final its value can not be changed like

public class Final {

    private final int b;

    Final(int b) {
        this.b = b; 
         


        
11条回答
  •  旧巷少年郎
    2020-12-09 05:27

    It means that the hasmap cannot be changed. The elements inside the hashmap are not tied to the final delimiter.

    private static final HashMap map = new HashMap();
    
    public void foo(K k, V v) {
        map.push(k, v);  //This is allowed
        map = new HashMap  //This is not allowed
    }
    

提交回复
热议问题