日期:2014-05-20  浏览次数:20618 次

为什么编译时会出非法向前引用的错误
class   Cat{
  public   String   name;
  public   int   age;
 
  public   Cat(String   name,int   age){
 
    System.out.println( "Constructor ");
    this.name   =   name;
    this.age   =   age;
  }
 
{
    System.out.println( "non-static   initial   block ");
      //   这边为什么不报向前引用的错误
      weight   =   2.0;
                  //   注释下面的语句编译可以通过
[color=#FF0000]System.out.println(weight);[/color]   }
  double   weight   =2.3   ;
  public   String   toString(){
    return   "Cat[name= "   +   name   + "   ,age=   "   +   age     + "   ,weight=   "   +   weight;
  }
}


public   class   InitTest{
  public   static   void   main(String[]   args){
    Cat   cat   =   new   Cat( "kitty ",2);
    System.out.println(cat);
    Cat   c2   =   new   Cat( "Jefi ",3);
    System.out.println(c2);
  }
}


------解决方案--------------------
这是JAVA代码块
http://blog.csdn.net/lichaohn/article/details/5359240