国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

關(guān)于Java中類加載時對其屬性和代碼塊初始化順序的研究與測試

2019-11-17 04:15:16
字體:
供稿:網(wǎng)友

  關(guān)于類加載時對其內(nèi)部屬性,代碼塊初始化(通常是分配棧內(nèi)存)的順序的測試結(jié)果如下幾點:
 * 1,類的加載,這是由類加載器執(zhí)行的,該步驟將查找字節(jié)碼(通常是在classPath的類路徑下)但這種并非是必須的,并從這些字節(jié)碼中創(chuàng)建Class對象。
 * 2,鏈接,在鏈接階段將驗證類中的字節(jié)碼,為靜態(tài)域分配存儲空間,并且如果必須的話。將解析這個類創(chuàng)建的對其他類的所有引用。所有的帶有static關(guān)鍵字的成員變量和靜態(tài)代碼塊的存儲空間就是在這個時候分配的(其中他們的分配空間的順序是按照代碼的先后順序的)。
 * 3,初始化,如果該類具有超類,則對其初始化,執(zhí)行靜態(tài)初始化器和靜態(tài)初始化塊(對成員變量和靜態(tài)初始化塊初始化)。
 * 首先明確一點,就是不管類的構(gòu)造器是否對其成員變量進行手動賦值,類在加載的時候都會為其成員變量
 * 初始化的,并且它的初始化時機要比構(gòu)造器初始化早的多(構(gòu)造器初始化是發(fā)生在new對象的時候),
 * 對成員變量和靜態(tài)初始化塊初始化得順序按照當(dāng)前的代碼先后順序。

以下為測試代碼:

測試結(jié)果:

+ expand sourceview plaincopy to clipboardPRint?
public class StaticInitialization {  
 
    public static void main(String[] args) {  
        System.out.println("Creating new Cupboard() in main");  
        new Cupboard();  
        System.out.println("Creating new Cupboard() in main");  
        new Cupboard();  
        table.f2(1);  
        cupboard.f3(1);  
    }  
    static Table table = new Table();  
    static Cupboard cupboard = new Cupboard();  
}  
class Bowl {  
    static int i = 9;  
      
    {  
        //靜態(tài)初始化塊  
        System.out.println(i);  
    }  
      
    static {  
        //靜態(tài)代碼塊  
        System.out.println("static:" + i);  
    }  
    Bowl(int marker) {  
        System.out.println("Bowl(" + marker + ");");  
    }  
    void f1(int marker) {  
        System.out.println("f1(" + marker + ");");  
    }  
}  
class Table {  
    static Bowl bowl1 = new Bowl(1);  
    Table() {  
        System.out.println("Table();");  
        bowl2.f1(1);  
    }  
    void f2(int marker) {  
        System.out.println("f2(" + marker + ");");  
    }  
    static Bowl bowl2 = new Bowl(2);  
}  
class Cupboard {  
    Bowl bowl3 = new Bowl(3);  
    static Bowl bowl4 = new Bowl(4);  
    Cupboard() {  
        System.out.println("Cupboard();");  
        bowl4.f1(2);  
    }  
    void f3(int marker) {  
        System.out.println("f3(" + marker + ");");  
    }  
    static Bowl bowl5 = new Bowl(5);  

public class StaticInitialization {

    public static void main(String[] args) {
        System.out.println("Creating new Cupboard() in main");
        new Cupboard();
        System.out.println("Creating new Cupboard() in main");
        new Cupboard();
        table.f2(1);
        cupboard.f3(1);
    }
    static Table table = new Table();
    static Cupboard cupboard = new Cupboard();
}
class Bowl {
    static int i = 9;
   
    {
        //靜態(tài)初始化塊
        System.out.println(i);
    }
   
    static {
        //靜態(tài)代碼塊
        System.out.println("static:" + i);
    }
    Bowl(int marker) {
        System.out.println("Bowl(" + marker + ");");
    }
    void f1(int marker) {
        System.out.println("f1(" + marker + ");");
    }
}
class Table {
    static Bowl bowl1 = new Bowl(1);
    Table() {
        System.out.println("Table();");
        bowl2.f1(1);
    }
    void f2(int marker) {
        System.out.println("f2(" + marker + ");");
    }
    static Bowl bowl2 = new Bowl(2);
}
class Cupboard {
    Bowl bowl3 = new Bowl(3);
    static Bowl bowl4 = new Bowl(4);
    Cupboard() {
        System.out.println("Cupboard();");
        bowl4.f1(2);
    }
    void f3(int marker) {
        System.out.println("f3(" + marker + ");");
    }
    static Bowl bowl5 = new Bowl(5);
}   


輸出結(jié)果:

 

9
Bowl(1);
9
Bowl(2);
Table();
f1(1);
9
Bowl(4);
9
Bowl(5);
9
Bowl(3);
Cupboard();
f1(2);
Creating new Cupboard() in main
9
Bowl(3);
Cupboard();
f1(2);
Creating new Cupboard() in main
9
Bowl(3);
Cupboard();
f1(2);
f2(1);
f3(1);


具體java虛擬機是怎么管理內(nèi)存的,和它對堆和棧是如何處理的,還需請教高手指點!
-------此文章僅為本人Java的復(fù)習(xí)總結(jié)


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 诸暨市| 栾城县| 兰溪市| 武安市| 长沙市| 靖远县| 大港区| 礼泉县| 福鼎市| 通州区| 柞水县| 大庆市| 舞钢市| 油尖旺区| 津市市| 怀集县| 三明市| 微山县| 常宁市| 辽阳市| 科技| 寻乌县| 密山市| 桂阳县| 尼玛县| 乐平市| 璧山县| 辉南县| 济阳县| 云梦县| 腾冲县| 密云县| 元朗区| 乐山市| 循化| 铁力市| 汉源县| 木兰县| 石泉县| 南通市| 宜章县|