靜態塊 非靜態塊 構造器 執行的順序

順序:spa

父類靜態塊 > 子類靜態塊 > 父類 非靜態塊 > 父類構造器 >  子類非靜態塊 > 子類構造器。code

Example:blog

public class BlockParentTest {

    public BlockParentTest() {
        System.out.println("parent constructor");
    }
    
    static {
        System.out.println("parent static block");
    }
    
    {
        System.out.println("parent block");
    }
}
public class BlockTest extends BlockParentTest{

    public BlockTest() {
        System.out.println("child constructor");
    }
    
    {
        System.out.println("child block");
    }
    
    static {
        System.out.println("child static block");
    }
    public static void main(String[] args) {
        new BlockTest();
    }
}

Result:class

parent static block
child static block
parent block
parent constructor
child block
child constructorstatic

相關文章
相關標籤/搜索