How to change a variable from another class in Java?
I am trying to change a variable from another class and use it in the first class.
I make a variable in the First class and give it a value of 1. Then I try to change the value of the same variable to 2 in the Second class, but it returns to 1 when I use it in the First class.
I am new to Java and still do not know very much, so if you could try to keep simple answers, that would be great :)
First grade:
public class First {
public static void main(String args[]){
int var = 1;
Second Second = new Second();
System.out.println(var);
Second.test(var);
System.out.println(var);
}
}
Second class:public class Second {
void test(int var){
var = 2;
System.out.println(var);
}
}
What the output looks like:
1
2
1
What am I trying to get:
1
2
2
Ive tried to find the answers to this, but all the answers that I could find made no sense to me, since they are very new to Java and programming.