如何从 Java 中的不同类更改变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21668759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I change a variable from a different class in Java?
提问by hhaslam11
How do I change a variable from a different class in Java?
如何从 Java 中的不同类更改变量?
I'm trying to change a variable from another class, and use it back in the first class.
我正在尝试从另一个类更改变量,然后在第一类中使用它。
I make a variable in class First, and give it a value of 1. Then I try to change the value of the same variable to 2 in class Second, but it changes back to 1 when I use it in class First.
我在类 First 中创建了一个变量,并将其赋值为 1。然后我尝试在类 Second 中将同一变量的值更改为 2,但是当我在类 First 中使用它时,它又变回了 1。
I'm new to Java and don't know very much yet, so if you could try and keep the answers simple, that would be great :)
我是 Java 新手,还不太了解,所以如果你能试着让答案保持简单,那就太好了:)
Class First:
第一课:
public class First {
public static void main(String args[]){
int var = 1; //Variable i'm trying to change from class "Second"
Second Second = new Second();
System.out.println(var); //Prints out 1
Second.test(var);
System.out.println(var); // Prints out 1 again, even though I changed it
}
}
Class Second:第二课:
public class Second {
void test(int var){
/*
*
* I try to change var to 2, and it works in this class
* but when it doesn't change in the class "First"
*
*/
var = 2;
System.out.println(var); //Prints out 2
}
}
What the output looks like:
输出结果如下:
1
2
1
1
2
1
What i'm trying to get:
我想得到什么:
1
2
2
1
2
2
Ive tried to find answers to this, but all of the answers that I could find didnt make any sense to me, as im very new to Java and programming.我试图找到答案,但我能找到的所有答案对我来说都没有任何意义,因为我对 Java 和编程非常陌生。
采纳答案by aliteralmind
The problem is
问题是
Second.test(var);
It's not a bug. It's just not doing what you think its doing.
这不是一个错误。它只是没有做你认为它在做的事情。
A primitive (an int
is called a primitive...it's not an object) passed to a function may be changed in that function, but in a copy. As soon as the function is done, the original value is the same, because it was never altered to begin with.
int
传递给函数的基元(an称为基元……它不是对象)可以在该函数中更改,但在副本中。一旦函数完成,原始值是相同的,因为它从未被更改为以 开头。
What you want is
你想要的是
int test(int var){
var = 2;
System.out.println(var); //Prints out 2
return var;
}
And then instead of
然后代替
Second.test(var);
use
用
var = Second.test(var);
There is actually no point in the parameter at all. It is equivalent to
参数中实际上根本没有任何意义。它相当于
var = Second.test();
...
int test(){
int var = 2;
System.out.println(var); //Prints out 2
return var;
}
I hope this helps. Good luck, welcome to Java, and welcome to stackoverflow!
我希望这有帮助。祝你好运,欢迎来到 Java,欢迎来到 stackoverflow!
回答by Christian
Java is pass-by-value, it means that you are passign a copy of var
. What you can do is to make method test()
to return a int
Java 是pass-by-value,这意味着你是 passign 的一个副本var
。你可以做的是让方法test()
返回一个int
int test(int var){
...
var = 2;
System.out.println(var); //Prints out 2
return var;
}
and then assign its result (the value the method test()
returns) to var
:
然后将其结果(方法test()
返回的值)分配给var
:
var = second.test(var);
System.out.println(var); //Prints out 2
Note:
笔记:
In
在
Second Second = new Second();
you shouldn't use the name of a class as a name of a variable. Do this sintead:
您不应该使用类的名称作为变量的名称。这样做:
Second second = new Second();
回答by Weibo Li
The var
parameter in test
method of class second
is a copy, not a reference or pointer. So you can not change the value of var
in class First
.
类的方法中的var
参数是副本,而不是引用或指针。所以你不能改变in class的值。test
second
var
First
To achieve your goal, you could declare var
as a global variable, like this:
为了实现您的目标,您可以声明var
为全局变量,如下所示:
public class First {
public int var = 1;
public static void main(String args[]){
Second Second = new Second();
System.out.println(var);
Second.test();
System.out.println(var);
}
}
public class Second {
void test(){
/*
*
* I try to change var to 2, and it works in this class
* but when it doesn't change in the class "First"
*
*/
First.var = 2;
}
}
回答by msmolcic
Well you've changed it in second class, but you did not return it back to the first class. First class can't possibly know you've changed it in the other class if you don't return it. You can do it this way, hope it helps.
嗯,你在二等舱改变了它,但你没有把它放回一等舱。如果您不归还,头等舱就不可能知道您在其他班级中更改了它。你可以这样做,希望它有帮助。
First class:
第一课:
public class First {
public static void main(String args[]){
int var = 1;
Second Second = new Second();
System.out.println(var); //Prints out 1
var = Second.test(var);
System.out.println(var); //Prints out 2
}
}
Second class:
第二类:
public class Second {
int test(int var){
var = 2;
System.out.println(var); //Prints out 2
return var;
}
}