代码一
package sty;
import java.util.HashMap;
import java.util.Map;
public class sty {
public static void main(String[] args) {
int i = 0;
while (true){
String str = "张三" + i;
dw.stringMap.put(i++,str);
try {
System.out.println(dw.stringMap.size());
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package sty;
import java.util.HashMap;
import java.util.Map;
/**
* 问题描述:
* 我需要在sty类中修改本类中的stringMap
* 但是sty中修改后,dw类中的Map并没有改变
* 因此我要怎么修改才能达到在另一个类中修改本类中的变量
* 并且修改是生效的
*/
public class dw {
public static Map<Integer,String> stringMap = new HashMap<Integer,String>();
public static void main(String[] args) {
while (true){
try {
System.out.println(stringMap.size());
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
我在代码一中修改代码二中的静态变量,在不同类中访问结果不同,代码一中访问后已经修改,代码二中却是没有修改的值。