国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

java基礎-靜態,非靜態(構造)代碼塊,類加載

vvpale / 2275人閱讀

摘要:想來想去先來一題比較好輸出結果分析類初始化順序父類靜態變量父類靜態代碼塊子類靜態代碼塊父類非靜態變量父類非靜態代碼塊父類構造函數子類非靜態變量子類非靜態代碼塊子類構造函數非靜態代碼塊輸出結果順序非靜態代碼塊被編譯器拷貝到了構造塊內所以稱

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

分析

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;

Java類初始化順序
父類靜態變量  ——>父類靜態代碼塊——>子類靜態代碼塊——>父類非靜態變量  ——>  
父類非靜態代碼塊——>父類構造函數 ——>子類非靜態變量——>子類非靜態代碼塊——>
子類構造函數
非靜態代碼塊 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

相關文章

  • JAVA快速復習

    摘要:八種基本數據類型數組定義數組元素類型數組名元素類型元素個數和數組長度元素類型數組名元素類型元素元素元素元素內存的劃分寄存器本地方法區方法區棧內存存儲局部變量變量所屬作用域一旦結束變量自動釋放方法進棧局部變量屬于方法所以方法要先進棧堆內存存儲 八種基本數據類型 byte short int long boolean char float double JAVA數組 定義數組 元...

    cppowboy 評論0 收藏0
  • Java初始化順序

    摘要:對子類成員數據按照它們聲明的順序初始化,執行子類構造函數的其余部分。參考類的初始化順序引了大半類加載的時機 jvm系列 垃圾回收基礎 JVM的編譯策略 GC的三大基礎算法 GC的三大高級算法 GC策略的評價指標 JVM信息查看 GC通用日志解讀 jvm的card table數據結構 Java類初始化順序 Java對象結構及大小計算 Java的類加載機制 Java對象分配簡要流程 年老...

    boredream 評論0 收藏0
  • 一道面試題引發的思考:(1)

    這是網易2015校招Java面試題,直接上題目。 題目 package com.mousycoder.staticTest; public class HelloB extends HelloA { public HelloB() { System.out.println(HelloB); } { System.out.println(I...

    tommego 評論0 收藏0
  • java基礎小記

    摘要:看到的只是,而由泛型附加的類型信息對來說是不可見的。然后再加載執行類的靜態變量以及靜態語句塊。接口中基本數據類型為而抽類象不是的。本地方法接口主要是調用或實現的本地方法及返回結果。用戶自定義類加載器,在程序運行期間,通過的子類動態加載。 編譯機制  編譯主要是把?.Java文件轉換為 .class 文件。其中轉換后的 .class 文件就包含了元數據,方法信息等一些信息。比如說元數據就...

    ruicbAndroid 評論0 收藏0
  • Java代碼執行順序

    摘要:沒有關鍵字修飾的如實例變量非靜態變量非靜態代碼塊初始化實際上是會被提取到類的構造器中被執行的,但是會比類構造器中的代碼塊優先執行到,非靜態實例變量非靜態代碼塊的地位是相等的,它們將按順序被執行。 閱讀原文:Java代碼執行順序 程序中代碼執行的順序非常重要,稍有不慎便會是程序運行出錯,那么我將結合實例來分析代碼中的執行。 名詞解釋 首先了解幾個名詞: 非靜態代碼塊 直接由 { } 包起...

    hosition 評論0 收藏0

發表評論

0條評論

vvpale

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<