I defined two java class test1 and test2, in which a variable a=3
was defined in test1, and then changed to 10
in the connect() method, and I used Scanner
to make the program not exit. But when I call test1.a
in test2, the value obtained is still the initial value a=3
instead of a=10
. What method should I use to get the modified value of a
?
public class test1 {
public static int a=3;
public static void connect(){
System.out.println(a);
a=10;
System.out.println(a);
Scanner sc = new Scanner(System.in);
sc.nextLine();
}
public static void main(String[] args) {
connect();
}
}
public class test2 {
public static void main(String[] args) {
System.out.println(test1.a);
}
}