初始化-主動(dòng)使用實(shí)例分析

根據(jù)上一節(jié)的理論我們知道,每個(gè)類的接口被Java程序“首次主動(dòng)使用”時(shí)才初始化,這一節(jié)我們就通過具體的實(shí)例來驗(yàn)證一下

  1. 創(chuàng)建類的實(shí)例
public class MyTest1 {
    public static void main(String[] args) {
        new MyParent1();
    }
}
class MyParent1 {
    public static String str = "hello world";

    static {
        System.out.println("MyParent static block");
    }
}

運(yùn)行程序,輸出:

MyParent static block

類的靜態(tài)代碼塊被執(zhí)行了,說明類進(jìn)行了初始化

  1. 訪問某個(gè)類或接口的靜態(tài)變量,或者對(duì)該靜態(tài)變量賦值
public class MyTest1 {
    public static void main(String[] args) {
        System.out.println(MyParent1.str);
    }
}
class MyParent1 {
    public static String str = "hello world";

    static {
        System.out.println("MyParent static block");
    }
}

運(yùn)行程序,輸出:

MyParent static block
hello world
  1. 調(diào)用類的靜態(tài)方法
public class MyTest1 {
    public static void main(String[] args) {
        MyParent1.print();
    }
}
class MyParent1 {
    public static String str = "hello world";

    public static void print(){
        System.out.println(str);
    }

    static {
        System.out.println("MyParent static block");
    }
}

運(yùn)行程序,輸出:

MyParent static block
hello world
  1. 反射
public class MyTest1 {
    public static void main(String[] args) throws ClassNotFoundException {
        Class.forName("com.shengsiyuan.jvm.classloader.MyParent1");
    }
}
class MyParent1 {
    public static String str = "hello world";

    static {
        System.out.println("MyParent static block");
    }
}

運(yùn)行程序,輸出:

MyParent static block
  1. 初始化一個(gè)類的子類
public class MyTest1 {
    public static void main(String[] args) throws ClassNotFoundException {
        new MyChild1();
    }
}
class MyParent1 {
    public static String str = "hello world";

    static {
        System.out.println("MyParent static block");
    }
}

class MyChild1 extends MyParent1 {
    public static String str2 = "welcome";

    static {
        System.out.println("MyChild1 static block");
    }
}

運(yùn)行程序,輸出:

MyParent static block
MyChild1 static block
  1. Java虛擬機(jī)啟動(dòng)時(shí)被表明為啟動(dòng)類的類
public class MyTest1 {
    static {
        System.out.println("MyTest1 static block");
    }
    public static void main(String[] args) throws ClassNotFoundException {
        
    }
}

運(yùn)行程序,輸出:

MyTest1 static block

非主動(dòng)使用注意點(diǎn)

以上實(shí)例都是對(duì)主動(dòng)使用的驗(yàn)證,我們來看一下下面這個(gè)程序

public class MyTest1 {
  public static void main(String[] args) throws ClassNotFoundException {
      System.out.println(MyChild1.str);
  }
}
class MyParent1 {
  public static String str = "hello world";

  static {
      System.out.println("MyParent static block");
  }
}

class MyChild1 extends MyParent1 {
  static {
      System.out.println("MyChild1 static block");
  }
}

運(yùn)行程序,輸出:

MyParent static block
hello world

我們可以看到,MyChild1 static block并沒有打印出來,這里我們調(diào)用MyChild1.str明明是對(duì)類的靜態(tài)變量的訪問,但是MyChild1卻沒有被初始化,所以這里要注意的一點(diǎn)就是:

對(duì)于靜態(tài)字段來說,只有直接定義了該字段的類才會(huì)被初始化


參考資料:
圣思園JVM課程

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評(píng)論 19 139
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,623評(píng)論 18 399
  • 6.6日咖啡冥想 我的目標(biāo)是帶領(lǐng)C2月業(yè)績(jī)上百萬 所以我的咖啡冥想內(nèi)容是: 1,把自己上個(gè)月帶團(tuán)隊(duì)的過程中的經(jīng)驗(yàn)進(jìn)...
    艷敏姐閱讀 409評(píng)論 0 0
  • 分享一下我的所見所聞,再加上我的所感。 我發(fā)現(xiàn)我們身邊的朋友們對(duì)于外國(guó)人都有一種神秘感。我覺得其實(shí)都是人類,都有人...
    長(zhǎng)春藤0805閱讀 405評(píng)論 0 1
  • 無論受了什么傷 都請(qǐng)?jiān)谀且惶靸?nèi)結(jié)束悲傷 你明白的 這個(gè)城市的車水馬龍 從來都不會(huì)在乎你昨天的泣不成聲
    無言以若閱讀 285評(píng)論 0 0

友情鏈接更多精彩內(nèi)容