-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInitOrder.java
More file actions
82 lines (66 loc) · 2.03 KB
/
InitOrder.java
File metadata and controls
82 lines (66 loc) · 2.03 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
@SuppressWarnings("unused")
public class InitOrder {
/**
* Java初始化次序:
* 父类静态变量、静态初始化块依次执行(首次加载时)
* 子类静态变量、静态初始化块依次执行(首次加载时)
* 父类实例变量、实例初始化块依次执行
* 父类构造方法
* 子类实例变量、实例初始化块依次执行
* 子类构造方法
*/
public static void main(String[] args) {
System.out.println("--- First time ---");
Sub sub1 = new Sub();
System.out.println("\n--- Second time ---");
Sub sub2 = new Sub();
}
}
@SuppressWarnings("unused")
class Base {
static String baseStaticStr1 = printAndReturn("Base static var 1");
String baseStr1 = printAndReturn("Base instance var 1");
static {
System.out.println("Base static init 1");
}
{
System.out.println("Base instance init 1");
}
public Base() {
System.out.println("Base constructor ***");
}
static String baseStaticStr2 = printAndReturn("Base static var 2");
String baseStr2 = printAndReturn("Base instance var 2");
static {
System.out.println("Base static init 2");
}
{
System.out.println("Base instance init 2");
}
public static String printAndReturn(String str) {
System.out.println(str);
return str;
}
}
@SuppressWarnings("unused")
class Sub extends Base {
static String subStaticStr1 = printAndReturn("Sub static var 1");
String subStr1 = printAndReturn("Sub instance var 1");
static {
System.out.println("Sub static init 1");
}
{
System.out.println("Sub instance init 1");
}
public Sub() {
System.out.println("Sub constructor ***");
}
static String subStaticStr2 = printAndReturn("Sub static var 2");
String subStr2 = printAndReturn("Sub instance var 2");
static {
System.out.println("Sub static init 2");
}
{
System.out.println("Sub instance init 2");
}
}