public class OrderTest{
int test = 1;
{
int test =2; //怎么不报错呢? System.out.println("test works! " + test); //输出为2
}
public OrderTest(){
System.out.println("testing result is "+ test); //输出为1
} public static void main(String[] args){
new OrderTest();
}
}
这个初始化块中的声明怎么回事呢??

解决方案 »

  1.   

    Every local variable declaration statement is immediately contained by a block.
    method, constructor, or initializer block ,and for statementHowever, a local variable of a method or initializer block may be shadowed (§6.3.1) anywhere inside a class declaration nested within the scope of the local variable. 意思就是:初始化快里面的是局部变量,局部变量可以被类的实例变量shadowed掉http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.4
      

  2.   

    编译原理的access link 存取链