java建立對象的static塊與構造器的執行順序

來源:互聯網
上載者:User

標籤:amp   print   使用   類構造   normal   執行個體   靜態代碼塊   csu   []   

前言:本文解決的問題

  • 建立一個對象靜態代碼塊什麼時候執行
  • {}裡面的代碼什麼時候執行
  • 有繼承關係時的執行順序
1.問題出現的背景:

構造器是用來執行個體化一個對象,當我們使用new關鍵字來建立對象時,構造器就會被調用。如果class中含有靜態代碼塊(static)和普通代碼塊(在{}括弧下),建立對象時的調用順序是:靜態代碼塊>{裡面的代碼}>構造器

2.例子說明:2.1代碼說明
//父類public class StaticExample{     {        System.out.println("***This is normal block***");    }        static {        System.out.println("This is first static block");    }    public StaticExample(){        System.out.println("This is constructor");    }           public static String staticString = "Static Variable";    static {        System.out.println("This is second static block and "                                                    + staticString);    }       static {        staticMethod();        System.out.println("This is third static block");    }    public static void staticMethod() {        System.out.println("This is static method");    }    public static void staticMethod2() {        System.out.println("This is static method2");    }} //子類public class StaticSubClass extends StaticExample{    static {        System.out.println("This is the subclass static block");    }    {        System.out.println("***This is the initialise block in subcluss***");    }    public StaticSubClass() {        System.out.println("This is the subclass constructor");    }//測試    public static void main(String[] args){        StaticSubClass statEx = new StaticSubClass();        StaticExample.staticMethod2();    }}   
2.1運行結果

This is first static block
This is second static block and Static Variable
This is static method
This is third static block
This is the subclass static block
This is initialise block base
This is base constructor
This is the initialise block in subcluss
This is the subclass constructor
This is static method2

3.結果分析:3.1static代碼塊先執行

先分別執行基類的兩個靜態代碼塊,然後執行父類的靜態代碼塊,由於父類靜態代碼塊中含有靜態方法,因此執行staticMethod(),然後再執行子類的靜態代碼塊,輸出為:

This is first static block
This is second static block and Static Variable
This is static method
This is third static block
This is the subclass static block

3.2建立父類,執行初始化代碼塊

執行完初static代碼塊後,再執行父類初始化代碼塊,然後執行父類的構造器,輸出為:

This is initialise block base
This is base constructor

3.3建立子類,執行初始化代碼塊

輸出為:

This is the initialise block in subcluss
This is the subclass constructor

3.3執行子類中的方法

最後調用繼承父類的方法staticMethod2(),輸出:

This is static method2

4.總結

使用new關鍵字建立對象時,

  • 首先執行static代碼塊中的,當存在繼承關係時,限制性父類的static代碼塊,然後執行子類的static
  • 其次執行{}初始化代碼塊,當有繼承關係時,執行父類的初始代碼塊,然後執行父類構造器。
  • 最後執行構造器
5.關於類中static代碼塊注意事項
  • 不能使用this關鍵字,因為還沒有執行個體化(You cannot use the this keyword since there is no instance.)
  • 也不能使用super(You shouldn’t try to access super since there is no such a thing for static blocks.)
  • 不要傳回值(You should not return anything from this block.)

更多請參考

java建立對象的static塊與構造器的執行順序

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.