当我将变量的值传递给方法时,变量的值没有得到更新?
The value of a variable does not get updated when I pass that to a method to do so?
package com.mkyong.test;
public class Main {
public static void main(String[] args) {
String something = "";
callSomething(something);
System.out.println(something);
}
private static String callSomething(String something) {
something = "Hello Wrold !";
return something;
}
}
不,在该方法中,您正在更改局部变量的引用。
将您的方法调用更改为:
something = callSomething(something);
package com.mkyong.test;
public class Main {
public static void main(String[] args) {
String something = "";
callSomething(something);
System.out.println(something);
}
private static String callSomething(String something) {
something = "Hello Wrold !";
return something;
}
}
不,在该方法中,您正在更改局部变量的引用。
将您的方法调用更改为:
something = callSomething(something);