更改通过参数传递的值
Changing the value being passed through the parameter
我正在测试是否可以通过传递参数来更改静态变量 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);
}
}
这意味着您必须直接引用静态变量才能更改它。好的。现在。我的问题是,有没有办法通过仅通过参数传递它来更改 class 变量,而无需在实现中引用它?基本上,有没有办法以某种方式使用第一种方式?谢谢
Java 始终将基元作为值传递。另一方面,对象总是作为引用传递。在第二个示例中,您访问了静态属性 x
而不是参数 x_
。
此外,static
不保护属性不被重写。它将属性绑定到 class(没有 static
属性绑定到对象)。也许你的意思是 final
?
编辑:更正了一个打字错误。
您可以实现您所要求的,但您需要注意细微的限制。您可以修改传递给方法的对象引用,但不能重新分配方法中的引用并更改原始对象。
Java 总是 按值传递。时期。
对象引用按值传递,但正是通过这些引用(指向相同的内存位置),您可以通过参数修改方法内部的对象。
您不能以这种方式修改基元(int
、float
、boolean
等),它们总是按值传递.您也不能修改不可变对象(例如 String
),因为它们不能使用 public 接口进行更改。
考虑这个简单的示例,它测试创建一个具有一些可修改字段、按值和按引用(它通过对象引用修改,它本身按值传递)的对象:
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
我正在测试是否可以通过传递参数来更改静态变量 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);
}
}
这意味着您必须直接引用静态变量才能更改它。好的。现在。我的问题是,有没有办法通过仅通过参数传递它来更改 class 变量,而无需在实现中引用它?基本上,有没有办法以某种方式使用第一种方式?谢谢
Java 始终将基元作为值传递。另一方面,对象总是作为引用传递。在第二个示例中,您访问了静态属性 x
而不是参数 x_
。
此外,static
不保护属性不被重写。它将属性绑定到 class(没有 static
属性绑定到对象)。也许你的意思是 final
?
编辑:更正了一个打字错误。
您可以实现您所要求的,但您需要注意细微的限制。您可以修改传递给方法的对象引用,但不能重新分配方法中的引用并更改原始对象。
Java 总是 按值传递。时期。 对象引用按值传递,但正是通过这些引用(指向相同的内存位置),您可以通过参数修改方法内部的对象。
您不能以这种方式修改基元(int
、float
、boolean
等),它们总是按值传递.您也不能修改不可变对象(例如 String
),因为它们不能使用 public 接口进行更改。
考虑这个简单的示例,它测试创建一个具有一些可修改字段、按值和按引用(它通过对象引用修改,它本身按值传递)的对象:
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