我正在测试你是否可以通过传递参数来改变静态变量x的值,但我发现你不能这样做。
public class Test {
static int x;
static void changeX(int x_) {
x_ = 50;
}
public static void main(String args[]) {
changeX(x);
System.out.println(x);//it prints out zero because the static variable did not get changed
}
}
如果我们这样做,我们可以改变它:
public class Test {
static int x;
static void changeX(int x_) {
x = 50;
}
public static void main(String args[]) {
changeX(x);
System.out.println(x);
}
}
这意味着您必须直接引用静态变量才能进行更改。好的。现在。我的问题是,有没有办法通过传递参数来更改类变量,而不在实现中引用它?基本上,有没有办法以某种方式使用第一种方式?感谢。
答案 0 :(得分:1)
你可以实现你的要求,但你需要了解微妙的限制。您可以修改传递给方法的对象引用,但不能重新分配方法中的引用并更改原始对象。
Java 始终按值传递。期。 对象引用是按值传递的,但是通过这些引用(指向相同的内存位置),您可以通过参数修改方法内的对象。
您无法以这种方式修改基元(int
,float
,boolean
等),它们始终按值传递。您也无法修改不可变对象(例如String
),因为它们无法使用公共接口进行更改。
考虑这个简单的例子,它测试创建一个具有一些可修改字段,按值和按引用的对象(它通过对象引用进行修改,它本身是按值传递的):
public class ParameterPassingTest {
static SomeObject someStaticObject;
public static void main(String[] args) {
// initialize a static object reference
someStaticObject = new SomeObject("I am a static Object", 10);
System.out.println("My static object before: " + someStaticObject);
// try modifying the reference by value
modifySomeObjectByValue(someStaticObject);
// try printing the value, it will be the same
System.out.println("My static object after mod-by-value: " + someStaticObject);
// now, try modifying by object reference
modifySomeObjectByReference(someStaticObject);
// print again. new values should be observed
System.out.println("My static object after mod-by-reference: " + someStaticObject);
}
// this method tries to modify the original object by assigning directly to the method parameter. It won't work.
public static void modifySomeObjectByValue(SomeObject someObject) {
SomeObject newObject = new SomeObject("I am another object, from a local method", 20);
someObject = newObject; // try to modify the original object by assigning to the parameter directly
}
// this method tries to modify the original object by using the object's public interface. It works.
public static void modifySomeObjectByReference(SomeObject someObject) {
// try to modify the original object by using the reference passed in
someObject.setaString("I have been modified by a method");
someObject.setAnInt(50);
}
}
// simple, generic class object with some fields.
class SomeObject {
String aString;
int anInt;
public SomeObject(String aString, int anInt) {
this.aString = aString;
this.anInt = anInt;
}
public String getaString() {
return aString;
}
public void setaString(String aString) {
this.aString = aString;
}
public int getAnInt() {
return anInt;
}
public void setAnInt(int anInt) {
this.anInt = anInt;
}
@Override
public String toString() {
return "aString = " + getaString() + " | anInt = " + getAnInt();
}
}
这会产生输出:
My static object before: aString = I am a static Object | anInt = 10
My static object after mod-by-value: aString = I am a static Object | anInt = 10
My static object after mod-by-reference: aString = I have been modified by a method | anInt = 50
答案 1 :(得分:-2)
Java始终将原语作为值传递。另一方面,对象总是作为参考传递。在第二个示例中,您访问的是静态属性x
,而不是参数x_
。
此外,static
不保护属性不被重写。它将属性绑定到类(没有static
属性绑定到对象)。也许你的意思是final
?
编辑:纠正错字。