您好,我有以下代码块
public class Driver {
static String x = "x";
static String y = "y";
public static void main(String[] args) throws Exception {
setX(x);
System.out.println("but x is still "+x);
}
static void setX(String x){
x="a";
System.out.println("now x should be = "+x);
}
}
然后打印
now x should be = a
but x is still x
我希望得到
now x should be = a
but x is still a
我知道有办法得到我想要的东西,但请回答为什么这不起作用。
最佳答案
static void setX(String x){
//x="a"; //refers to local variable , from parameter
//make it as follows
Driver.x="a";
System.out.println("now x should be = "+x);
}
关于java - Java 基本概念,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9835316/