Java中执行顺序

2014-11-13 23:15:04 · 作者: · 浏览: 30

  所以上面的例子打印顺序应该是这样的:


  parent static block 父类Static


  child static block 子类static


  parent block 父类缺省{}


  parent constructor 父类构造函数


  child block子类缺省{}


  child constructor子类构造函数


  class Parent{


  static String name = "hello";


  static {


  System.out.println("parent static block");


  }


  {


  System.out.println("parent block");


  }


  public Parent(){


  System.out.println("parent constructor");


  }


  }


  class Child extends Parent{


  static String childName = "hello";


  static {


  System.out.println("child static block");


  }


  {


  System.out.println("child block");


  }


  public Child(){


  System.out.println("child constructor");


  }


  }


  public class StaticIniBlockOrderTest {


  public static void main(String[] args) {


  new Child();//语句(*)


  }


  }


  编辑特别推荐: