Given the following code within a method: public class George
{
private static int x = 2; static void method1(int i)
{
x = i;
++i;
} public static void main(String[] args)
{
int i = 1;
method1(i);
System.out.println("x = " + x);
System.out.println("i = " + i);
}
}If the “main” method of this class is executed, the outputted values for “x” and “i”
respectively will be: (a) 1 and 1
(b) 1 and 2
(c) 2 and 1
(d) 2 and 2能给个解释吗?万分感谢
{
private static int x = 2; static void method1(int i)
{
x = i;
++i;
} public static void main(String[] args)
{
int i = 1;
method1(i);
System.out.println("x = " + x);
System.out.println("i = " + i);
}
}If the “main” method of this class is executed, the outputted values for “x” and “i”
respectively will be: (a) 1 and 1
(b) 1 and 2
(c) 2 and 1
(d) 2 and 2能给个解释吗?万分感谢
而第二个用直接是主方法里面的i也就是1,默认的,打印也是主方法里的i,和在method1方法没任何关系,它也不返回任何对i的处理
你可以试着在静态方法里随便给i赋值,结果无论如何都是1