一道關於加載順序的題目分析

package com.gt.world.oa.aaa;

/**
 *
 * @author GT
 */
public class Test20140331 {
    public static int k = 0;
    public static Test20140331 t1 = new Test20140331("t1");
    public static Test20140331 t2 = new Test20140331("t2");
    public static int i = print("i");
    public static int n = 99;
     
    public int j = print("j");
     
    {
        print("構造塊");
    }
     
    static {
        print("靜態塊");
    }
 
    public Test20140331(String str) {
        System.out.println((++k) + ":" + str + "   i=" + i + "    n=" + n);
        ++i;
        ++n;
    }
 
    private static int print(String str) {
        System.out.println((++k) + ":" + str + "   i=" + i + "   n=" + n);
        ++n;
        return ++i;
    }
 
    public static void main(String[] args) {
        Test20140331 t = new Test20140331("init");
        
        Test20140331 t2 = new Test20140331("init2");
    }
 
}

/**
 *  
 *  1 程序從main函數開始執行 ,執行main函數,需要先加載class文件
 *    2 加載class文件的同時,同時初始化static成員變量和static塊,執行順序爲從上到下依次執行
 *    3 加載class完成之後,初始化成員變量。注:普通代碼塊,可以看作成員變量,執行順序爲從上到下依次執行
 *    4 上面的過程完成之後,再從main函數的第一條語句開始執行。
 *    5 注:靜態成員變量和靜態代碼塊只會 在加載class文件的時候 執行一次
 */

/**
 * public static Test20140331 t1 = new Test20140331("t1");
 * 1>  1:j         i=0    n=0
 * 2>  2:構造塊     i=1    n=1
 * 3>  3:t1        i=2    n=2
 *
 * public static Test20140331 t2 = new Test20140331("t2");
 * 4>  4:j         i=3    n=3
 * 5>  5:構造塊     i=4    n=4
 * 6>  6:t2        i=5    n=5
 *
 * public static int i = print("i");
 * 7>  7:i         i=6    n=6
 *
 * public static int n = 99;
 * 8>  n=99
 *
 *     static {
            print("靜態塊");
        }
 * 9>   8:靜態塊    i=7    n=99
 *
 * Test20140331 t = new Test20140331("init");
 * 10>  9:j         i=8    n=100
 * 11>  10:構造塊     i=9    n=101
 * 12>  11:init      i=10    n=102
 *
 *  * Test20140331 t = new Test20140331("init2");
 * 13>  12:j         i=11     n=103
 * 14>  13:構造塊     i=12     n=104
 * 15>  14:init2      i=13    n=105
 *
 */



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章