java虚拟机类加载机制

参考: Java虚拟机类加载机制

一个例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package test;

public class {
public static void main(String[] args) {
staticFunction();

}

// static int b = 112;
static StaticTest st = new StaticTest();

static {
System.out.println("1");
}

{
System.out.println("2");
}

StaticTest() {
System.out.println("3");
System.out.println("a=" + a + ",b=" + b);
}

public static void staticFunction() {
System.out.println("4");
}

int a = 110;
static int b = 112;
}

注意注释部分取消注释后程序输出的变化

解释参考: Java虚拟机类加载机制——案例分析