不能通过吧,大哥
我用JCreator3.0试的

解决方案 »

  1.   

    变量重复定义,通不过编译的{
     int x=12;
     {
      x=96;
      System.out.print(x);
     }
     System.out.println(x);
    }应该这样测试啊
      

  2.   

    楼主你真的动手试过了?
    我刚刚不信,所以我用eclipse试了一下,编译无法通过……
    提示信息为“局部变量 x 重复”。
      

  3.   

    源码如下:import java.awt.Graphics;
    import java.applet.Applet;
    public class Scoping extends Applet {
    int x=1;                //此处的全局x可以被下面出现的x给屏蔽掉。
    public void paint(Graphics g) {
    g.drawString("See command line for output", 25, 25 );
    int x=5;
    System.out.println("local x in paint is "+x);
    a();
    b();
    a();
    b();
        System.out.println("\nlocal x in paint is "+x);
    c();
    }
    void a() {
     int x=25;
    System.out.println("\nlocal x in a is "+x+" after entering a");

    ++x;
    System.out.println("local x in a is "+this.x+" before exiting a");
    }

    void b() {
    System.out.println("\ninstance variable x is "+x+" on entering b");

    this.x*=10;//原来是 x*=10;
    System.out.println("\ninstance variable x is "+this.x+" on entering b");
    }

    void c() {
    System.out.println("最后输出全局x:"+x);
    }
    }
    正如上面的代码所示,全局的int x=1; 能被paint()中的int x=5;给屏蔽掉,是不是当int x=1;是全局变量时就能给屏蔽掉?
      

  4.   

    楼主,你那程序是两码事了..你的第一个X是成员变量,第二个X是局部变量.而这样显然是在一个方法内定义的..本质区别.
    {
     int x=12;
     {
      int x=96;
     }
    }
      

  5.   

    public class ClassA{
    int x = 16;
    {
    int x = 24;
    }
    }楼主是这样写的? {
    int x = 24;
    }
    这是一个block,相当于一个方法,而不是声明,第二个X是局部变量
    如果写在方法里这样是肯定通不过的