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;
}
}求具体得到结果的过程!!

解决方案 »

  1.   

    public class Test {
    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是这样。
      

  2.   

    static {
    int x = 5;
    } 这是什么东东 啊 类 都没class啊 程序会走的下去吗  
      

  3.   

    y = x++ + ++x;// 初始x=-1,y=01.先把x++的值取出来 -1 ,之后x自增 = 0
    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;
    }
      

  4.   

    //这个静态代码块中的x相当于局部变量
    static {
    int x = 5;
    }
      

  5.   

    这是一个块,在执行main方法前执行
      

  6.   

    看看我的代码,中间有注释,绝对让你懂!
    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   */