摘要:想來想去先來一題比較好輸出結果分析類初始化順序父類靜態變量父類靜態代碼塊子類靜態代碼塊父類非靜態變量父類非靜態代碼塊父類構造函數子類非靜態變量子類非靜態代碼塊子類構造函數非靜態代碼塊輸出結果順序非靜態代碼塊被編譯器拷貝到了構造塊內所以稱
static block and non-static block(constructor block)
[toc]
想來想去,先來一題比較好public class Foo { public static void main(String[] args) { Baz.testAsserts(); Baz.testAsserts(); // Will execute after Baz is initialized. } } class Bar { static { Baz.testAsserts(); // Will execute before Baz is initialized! } } class Baz extends Bar { static int x = 1; static void testAsserts() { System.out.println("x is " + x); x=x+2; } }
輸出結果
x is 0
x is 1
x is 3
分析
Java類初始化順序Invoking Baz.testAsserts() cause Baz to be initialized
default value int x=0;
Before Baz initialize , Bar must be initialized
Bar"s static initializer again invoking Baz.testAsserts()
so x is 0 , then x+2 , x=2
go on initializing Baz , init x = 1;
Invoking Baz.testAsserts() x = 1 , so x is 1;
父類靜態變量 ——>父類靜態代碼塊——>子類靜態代碼塊——>父類非靜態變量 ——>非靜態代碼塊 non-static block(constructor block)
父類非靜態代碼塊——>父類構造函數 ——>子類非靜態變量——>子類非靜態代碼塊——>
子類構造函數
class A { int x ; //block num 1 { x = 1; System.out.println("block num 1, x is " + x); } A() { x = 3; System.out.println("constructor block x is " + x); } //block num 2 { x = 2; System.out.println("block num 2, x is " + x); } } public class Non_staticBlock { public static void main(String[] args) { String newLine = System.getProperty("line.separator"); System.out.println("====first time instantiate ====" + newLine); new A(); System.out.println(" ====second time instantiate ====" + newLine); new A(); } }
輸出結果、順序
====first time instantiate ====block num 1, x is 1
block num 2, x is 2
constructor x is 3====second time instantiate ====
block num 1, x is 1
block num 2, x is 2
constructor x is 3
非靜態代碼塊被java編譯器拷貝到了構造塊內,所以稱為"constructor block"也是可以的,所以每次 new 構造函數也都執行
.class 文件如下, 非靜態代碼塊被java編譯器拷貝到了構造塊內.
class A { int x = 1; A() { System.out.println("block num 1, x is " + this.x); this.x = 2; System.out.println("block num 2, x is " + this.x); this.x = 3; System.out.println("constructor x is " + this.x); } }靜態代碼塊 static block
class AA { AA() { x = 3; System.out.println("constructor x is " + x); } static int x = 1; //block num 1 static { System.out.println("static block num 1 , x is " + x); } //block num 2 static { x = 2; System.out.println("static block num 2 , x is " + x); } static void print() { System.out.println("static method"); } } public class StaticBlock { static { System.out.println("==== first ===="); } public static void main(String[] args) { String newLine = System.getProperty("line.separator"); System.out.println("====AA class init ====" + newLine); // class init AA.print(); System.out.println(" ====fisrt time instantiate AA====" + newLine); new AA(); System.out.println(" ====sencond time instantiate AA====" + newLine); new AA(); } }
輸出結果、順序
==== first ====
====AA class init ====static block num 1 , x is 1
static block num 2 , x is 2
static method
==== first time instantiate AA ====constructor x is 3
==== second time instantiate AA ====
constructor x is 3
由于JVM在加載類時會執行靜態代碼塊,且只會執行一次. 本例靜態引用AA.print(); 觸發類初始化
靜態代碼塊先于主方法執行,本例優先打印first
更多內容搜索jvm類加載
.class 文件如下
class AA { static int x = 1; AA() { x = 3; System.out.println("constructor x is " + x); } static void print() { System.out.println("static method"); } static { System.out.println("static block num 1 , x is " + x); x = 2; System.out.println("static block num 2 , x is " + x); } }聯合看一下
class AAA { int x; //block num 1 { x = 1; System.out.println("non-static block num 1 x is " + x); } AAA() { x = 3; System.out.println("constructor x is " + x); } //block num 2 { x = 2; System.out.println("non-static block num 2 x is " + x); } // The static block only gets called once,when the class itself is initialized, // no matter how many objects of that type you create static { System.out.println("static block"); } //Gets called every time an instance of the class is constructed. //the non-static block is actually copied by the Java compiler into every constructor the class has (source). //So it is still the constructor"s job to initialize fields. //to understand "actually " , find the result in the .class file of A.class { System.out.println("non-static block"); } } public class BlockSample { public static void main(String[] args) { String newLine = System.getProperty("line.separator"); System.out.println("====first time instantiate AAA ====" + newLine); new AAA(); System.out.println(" ====second time instantiate AAA ====" + newLine); new AAA(); } }
輸出結果、順序
====first time instantiate AAA ====static block
non-static block num 1 x is 1
non-static block num 2 x is 2
non-static block
constructor x is 3====second time instantiate AAA ====
non-static block num 1 x is 1
non-static block num 2 x is 2
non-static block
constructor x is 3
.class 文件
class AAA { int x = 1; AAA() { System.out.println("non-static block num 1 x is " + this.x); this.x = 2; System.out.println("non-static block num 2 x is " + this.x); System.out.println("non-static block"); this.x = 3; System.out.println("constructor x is " + this.x); } static { System.out.println("static block"); } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/68402.html
摘要:對子類成員數據按照它們聲明的順序初始化,執行子類構造函數的其余部分。參考類的初始化順序引了大半類加載的時機 jvm系列 垃圾回收基礎 JVM的編譯策略 GC的三大基礎算法 GC的三大高級算法 GC策略的評價指標 JVM信息查看 GC通用日志解讀 jvm的card table數據結構 Java類初始化順序 Java對象結構及大小計算 Java的類加載機制 Java對象分配簡要流程 年老...
這是網易2015校招Java面試題,直接上題目。 題目 package com.mousycoder.staticTest; public class HelloB extends HelloA { public HelloB() { System.out.println(HelloB); } { System.out.println(I...
摘要:看到的只是,而由泛型附加的類型信息對來說是不可見的。然后再加載執行類的靜態變量以及靜態語句塊。接口中基本數據類型為而抽類象不是的。本地方法接口主要是調用或實現的本地方法及返回結果。用戶自定義類加載器,在程序運行期間,通過的子類動態加載。 編譯機制 編譯主要是把?.Java文件轉換為 .class 文件。其中轉換后的 .class 文件就包含了元數據,方法信息等一些信息。比如說元數據就...
摘要:沒有關鍵字修飾的如實例變量非靜態變量非靜態代碼塊初始化實際上是會被提取到類的構造器中被執行的,但是會比類構造器中的代碼塊優先執行到,非靜態實例變量非靜態代碼塊的地位是相等的,它們將按順序被執行。 閱讀原文:Java代碼執行順序 程序中代碼執行的順序非常重要,稍有不慎便會是程序運行出錯,那么我將結合實例來分析代碼中的執行。 名詞解釋 首先了解幾個名詞: 非靜態代碼塊 直接由 { } 包起...
閱讀 1266·2021-10-14 09:50
閱讀 1567·2019-08-30 15:54
閱讀 1023·2019-08-30 11:22
閱讀 2916·2019-08-30 10:50
閱讀 1801·2019-08-29 18:39
閱讀 3050·2019-08-29 13:07
閱讀 2079·2019-08-28 17:54
閱讀 751·2019-08-26 17:44