为什么Java中的最终实例类变量? [英] Why final instance class variable in Java?

查看:113
本文介绍了为什么Java中的最终实例类变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果实例变量设置为最终,则其值无法更改,如

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; 
    }

    int getFinal() {
        return  b = 8;  // COMPILE TIME ERROR 
    }
}



代码中的某处我看到实例类变量 HashMap 被声明为最终

 private  final Map<String, Object> cacheMap = new HashMap<String, Object>();

我无法理解为什么会这样声明?通常在这种情况下它被声明。这是否意味着如果我放入哈希地图然后我无法更改其值?

编辑:

如果声明为final的cacheMap作为参数传递给另一个类,那么如果我更改其引用,则不会显示final的错误。为什么会这样?


If cacheMap which is declared as final is passed as parameter to another class then error is not shown for final if I change its reference. Why it is so?

 class CacheDTO {

    private Map conditionMap;

    public Map getConditionMap() {
        return conditionMap;
    }

    public void setConditionMap(Map conditionMap) {
        this.conditionMap = conditionMap;
    }
}

然后

private  final Map<String, Object> cacheMap = new HashMap<String, Object>();
CacheDTO cc = new CacheDTO();
cc.setConditionMap(cacheMap);
Map<String, Object> cacheMapDeclaredAsFinal = cc.getConditionMap();
Map<String, Object> newMap = new HashMap<String, Object>();
cacheMapDeclaredAsFinal = newMap;    // In this case no error is shown. Though cacheMapDeclaredAsFinal reference is obtained by calling cc.getConditionMap() and cacheMapDeclaredAsFinal refers to final.


推荐答案

您无法更改购物篮。你仍然可以改变里面的水果。

来自语言规范#14.12.4章


一旦指定了最终变量,它总是包含相同的值。如果final变量保存对象的引用,则对象的状态可能会被对象上的操作更改,但变量将始终引用同一个对象。

Once a final variable has been assigned, it always contains the same value. If a final variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object.

当您声明字段引用 final时,您必须设置值一旦构造函数退出。

When you declare a field or reference final, you must set the value once by the time the constructor exits.

只能在构造函数中为该变量赋值。

You can assign a value to that variable only in constructor.

 private  final Map<String,Object> CacheMap = new HashMap<String,Object>();

在这里你可以做到

CacheMap.put(.....  

在课堂上。

但你做不到

CacheMap =   something.  //compile error.

您应该知道值和参考

修改

这里

 Map<String, Object> cachemapdeclaredasfinal = cc.geConditionMap();

 Map<String, Object> newMap = new HashMap<String, Object>();

 cachemapdeclaredasfinal  = newMap; // In this case no error is shown

原因,

由于 cachemapdeclaredasfinal 不是新地图它是 conditionMap的另一个引用

当您创建这样的新实例时

when you create a new instance like this

   Map<String, Object> cachemapdeclaredasfinal =
                                new HashMap<String, Object>(cc.geConditionMap());

该错误消失。因为您使用了

That error disappears. since you used new.

编辑2:

 private Map conditionMap;

 public void setConditionMap(Map ConditionMap) {
        this.conditionMap = conditionMap;
    }
  private  final Map<String, Object> CacheMap = new HashMap<String, Object>();
  CacheDto cc = new CacheDto();
  cc.setConditionMap(CacheMap);
  Map<String, Object> cachemapdeclaredasfinal = cc.geConditionMap();
  Map<String, Object> newMap = new HashMap<String, Object>();
 cachemapdeclaredasfinal  = newMap;

在这里,你感到困惑的是。

Here you what you confused is.

您将一个 final 声明 map 分配给某些正常(非 final map 。当您检索到正常情况时,您只能获得 final ,因此您可以进一步使用/ 分配

You are assigning one final declared map to some normal(non final) map. When you retrieved that normal only you are getting and that not final so you can use/assign it further.

简短

normalMap= finalMap; //no error since normalMap is not final
finalMap =normalMap;// compiler error since normalMap is final

这篇关于为什么Java中的最终实例类变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆