转载请注明:文章转载自:聚资库?[http://www.juziku.com]
本文标题:java初始化顺序
本文地址:http://www.juziku.com/sunlightcs/wiki/3851.htm
?
java初始化顺序?
JAVA类首次装入时,会对静态成员变量或方法进行一次初始化,但方法不被调用是不会执行的。?
monospace !important; display: block !important;">01
class="keyword">class
?Test1 {
02
????
//3.初始化块
03
????
{
04
????????
System.out.print(
"3\n"
);
05
????
}
06
?
07
????
//4.父类构造器
08
????
Test1() {
09
????????
System.out.print(
"4\n"
);
10
????
}
11
?
12
????
//1.父类静态初始化块
13
????
static
?{
14
????????
System.out.print(
"1\n"
);
15
????
}
16
}
17
?
18
public
?class
?Test2?
extends
?Test1 {
19
????
//6.子类构造器
20
????
Test2() {
21
????????
System.out.print(
"6\n"
);
22
????
}
23
?
24
????
//2.子类静态初始化块
25
????
static
?{
26
????????
System.out.print(
"2\n"
);
27
????
}
28
????
//5.初始化块
29
????
{
30
????????
System.out.print(
"5\n"
);
31
????
}
32
?
33
????
public
?static
?void
?main(String[] args) {
34
????????
//即使newTest2实例,静态块初始化也进行
35
????????
new
?Test2();
36
????
}
37
}
?