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
*
*/
一道关于加载顺序的题目分析
最新推荐文章于 2021-02-11 14:10:26 发布
