简体   繁体   English

为什么静态hashmap在下面的情况下始终为null

[英]Why static hashmap is always null in below case

// sampleMap is always null even though first time initialized in addMap method //即使首次在addMap方法中初始化,sampleMap也始终为null

public class Sample { 公共类Sample {

private static HashMap<Long, Long> sampleMap;

public Sample() {
    addToMap(sampleMap, 100L, 100L);
    addToMap(sampleMap, 200L, 200L);
    addToMap(sampleMap, 300L, 300L);
    addToMap(sampleMap, 400L, 400L);
}

public HashMap<Long, Long> getSampleMap() {
    return sampleMap;
}

private void addToMap(HashMap<Long, Long> map, Long key, Long value) {
    if (map == null) {
        map = new HashMap<Long, Long>();
    }
    map.put(key, value);
}

public static void main(String[] args) {
    Sample obj = new Sample();
    obj.getSampleMap();
}

} }

addToMap creates a HashMap instance and assigns it to a local variable. addToMap创建一个HashMap实例并将其分配给局部变量。 It doesn't change the value of the sampleMap static variable. 它不会更改sampleMap静态变量的值。

Java is a pass by value language. Java是一种价值传递语言。 Therefore, when you pass a variable to a method, the method can't change the value of that variable. 因此,将变量传递给方法时,该方法无法更改该变量的值。 If the method assigns a new value to the variable, this assignment is local to the method, and doesn't change the value of the original variable passed to the method. 如果方法为变量分配新值,则此赋值对于方法是本地的,并且不会更改传递给方法的原始变量的值。

You have several options. 你有几个选择。 Here are some of them : 这里是其中的一些 :

Initialize sampleMap when it is declared : sampleMap时初始化sampleMap

private static HashMap<Long, Long> sampleMap = new HashMap<Long, Long>();

or initialize sampleMap before first calling addToMap : 或初始化sampleMap首先调用之前addToMap

public Sample() {
    sampleMap = new HashMap<Long, Long>();
    addToMap(sampleMap, 100L, 100L);
    addToMap(sampleMap, 200L, 200L);
    addToMap(sampleMap, 300L, 300L);
    addToMap(sampleMap, 400L, 400L);
}

or don't pass sampleMap to addToMap . 或者不要将sampleMap传递给addToMap addToMap can access it directly : addToMap可以直接访问它:

private void addToMap(Long key, Long value) {
    if (sampleMap == null) {
        sampleMap = new HashMap<Long, Long>();
    }
    sampleMap.put(key, value);
}

One last thing. 最后一件事。 It makes little sense for an instance method to return a static member. 实例方法返回静态成员没有多大意义。 Either make getSampleMap() static, or make sampleMap an instance variable (ie non static). 要么使getSampleMap()静态,要么使sampleMap成为实例变量(即非静态)。 The same is relevant for addToMap() . 这与addToMap()相关。

Java is always pass-by-value . Java始终是pass-by-value You can't reassign a reference inside a method. 您无法在方法内重新分配引用。

That being said, there is no need to pass map to the adMap method. 话虽这么说,没有必要将map传递给adMap方法。 Just use the static sampleMap directly : 只需直接使用static sampleMap

private void addToMap(Long key, Long value) {
    if (sampleMap== null) {
        sampleMap= new HashMap<Long, Long>();
    }
    sampleMap.put(key, value);
}

You can then alter your construcotr as : 然后,您可以将construcotr更改为:

public Sample() {
    addToMap(100L, 100L);
    addToMap(200L, 200L);
    addToMap(300L, 300L);
    addToMap(400L, 400L);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM