PackageCom.imooc.practice;classparent{ PublicParent () {System.out.println (The parent constructor method executes! "); } {System.out.println ("Parent non-static statement block execution!" "); } Static{System.out.println ("Parent Static statement block execution!" "); }} Public classChildextendsparent{ PublicChild () {System.out.println ("Child construction Method Execution!" "); } {System.out.println ("Child non-static statement block execution!"); } Static{System.out.println ("Child static statement block execution!" "); } Public Static voidMain (string[] args) {child C=NewChild (); }}
Object Initialization order: first executes the parent class static content, after the parent class static content executes, then executes the subclass static content, when the subclass static content executes completes, then goes to see the parent class has not the non-static code block, if has executes the non-static code block of the parent class, the parent class's non-static code block executes, The constructor of the parent class is then executed, and after the construction method of the parent class is executed, it goes on to see if the subclass has no non-static block of code, and if there is a non-static block of code that executes the subclass. The non-static code block of the subclass executes and then executes the subclass's construction method. In short, the static code block content executes first, then executes the parent class non-static code block and construction method, then executes the subclass non-static code block and constructs the method.
and the construction method of the subclass, regardless of the constructor method with no parameters, the default it will first look for the parent class without parameters of the construction method. If the parent class does not have a constructor with no arguments, the subclass must use the supper key to call the parent class with the constructor of the parameter, otherwise the compilation cannot pass.
Java STATIC statement block, statement block, construction method execution order