public class Test { static {
int x = 5;
}
static int x, y;
public static void main(String args[]) {
x--;
myMethod();
System.out.println(x + y + ++x);
}
public static void myMethod() {
y = x++ + ++x;
}
}求具体得到结果的过程!!
int x = 5;
}
static int x, y;
public static void main(String args[]) {
x--;
myMethod();
System.out.println(x + y + ++x);
}
public static void myMethod() {
y = x++ + ++x;
}
}求具体得到结果的过程!!
static {
int x = 5;//无效
}
static int x, y;//1. x=0,y=0
public static void main(String args[]) {
x--;//2. x=-1,y=0
myMethod();
System.out.println(x + y + //x=1,y=0
++x);//x=2,y=0
}
public static void myMethod() {
y = x++ //3. x=-1,y=0
+ ++x; //4. x=1,y=0
}
}
MS是这样。
int x = 5;
} 这是什么东东 啊 类 都没class啊 程序会走的下去吗
2.执行++x,之后x=1,再把1取出来
y =0 //步骤1+步骤2 System.out.println(x + y + ++x); //初始x=1,y=0
1.x+y+步骤2 // 1+0+2
2.执行++x,x=2,再把2取出来//作用域无效
static {
int x = 5;
}
static {
int x = 5;
}
public class Java03 {static {
int x = 5;
System.out.println("静态"+x);/*运行一次即停止,无任何后续动作,死代码。*/
}
static int x, y;
public static void main(String args[]) {
x--;
System.out.println(x);
myMethod();
System.out.println("main值x"+x);
System.out.println("main值y"+y);
System.out.println(x + y + ++x);
}
public static void myMethod() {
y = x++ + ++x;
System.out.println("myMethod值x"+x);
System.out.println("myMethod值y"+y);
}
}
/*根据我这个测试之后,发现一个问题,上面的那个静态块是死掉的,根本不会运行它。
而且,在main方法里调用的那个设置为static int 的x、y 都是0,x经过 “x--” x变成了-1,
而y却没有经过设置,还是0。 最后输出的 x+y+ ++x 也就变成了 x+ ++x ,结果=3 */