public
class
Outer {
2
3
4 public Outer() {
5 System.out.print("a"); // 構造方法,new的時候才會出現,且后於變量的創建
6 }
7
8
9
10 public static void sayOther(String s){
11 System.out.print(s);
12 }
13
14
15
16 public int say(String s){
17 System.out.print(s);
18 return 0;
19 }
20
21
22 // 初始化塊,在new時,構造方法之前,變量之前執行
23 {
24 System.out.print("c");
25 inner.innerMethed("d");
26 }
27
28
29 private static inner t= new inner(); // 靜態變量,這些都是在類加載完之前就放於內存中,且只加載這一次,new類對象時是不會再次執行的了。
30 static
31 {
32 System.out.print("e");
33 inner.innerMethed("f"); // 靜態初始化塊,整個靜態的都是在加載之前就做好初始化。
34 new inner();
35 }
36
37
38 private int i=say("g"); // 在new時,先於構造但是后於初始化塊的一個實例變量,也即:實例對象:(實例初始化塊,實例變量)--->構造方法
39 private inner tt= new inner(); // 整個是在類加載ClassLoader之后初始化變量時,才做。每個new對象都會執行上面這個步驟
40 private innerOther ttt= new innerOther();
41
42
43
44 static class inner{
45
46
47 public inner(){
48 System.out.print("h");
49 }
50
51
52 public static void innerMethed(String s){
53 System.out.print(s);
54 }
55
56
57
58 {
59 System.out.print("i");
60 }
61
62
63
64 static
65 {
66 System.out.print("j");
67 }
68
69
70 }
71
72
73
74 class innerOther{
75
76
77 public innerOther(){
78 System.out.print("k");
79 }
80
81
82 {
83 System.out.print("l");
84 }
85
86
87
88 }
89
90
91
92 public static void main(String[] args) {
93 System.out.print("m");
94 Outer outer = new Outer();
95 System.out.print("n");
96
97 // 總結就是:類對象,變量的加載順序
98 // 在整個類加載完成之后,就初始化靜態(靜態變量,靜態塊),其中可以new對象,別的類調用靜態方法。靜態的只做這一次,然后類加載器ClassLoader就把類Class返還,執行main方法。
99 // 在new類對象時,執行順序是:(實例初始化塊,實例變量)-->構造方法。每new一個對象時,都會執行這個步驟。
100
101 // (靜態變量、靜態初始化塊)—— 》 (變量、初始化塊)—— 》 構造函數,()中的內容是按照代碼前后循序執行
102
103 // 當有繼承關系時,還是基於這個原理,如new Son();
104 // 父類的靜態(塊和變量)-->子類的靜態(塊和變量)-->父類的(塊和變量+構造方法)-->子類的(塊和變量+構造方法)
105 //
106 }
107 }
2
3
4 public Outer() {
5 System.out.print("a"); // 構造方法,new的時候才會出現,且后於變量的創建
6 }
7
8
9
10 public static void sayOther(String s){
11 System.out.print(s);
12 }
13
14
15
16 public int say(String s){
17 System.out.print(s);
18 return 0;
19 }
20
21
22 // 初始化塊,在new時,構造方法之前,變量之前執行
23 {
24 System.out.print("c");
25 inner.innerMethed("d");
26 }
27
28
29 private static inner t= new inner(); // 靜態變量,這些都是在類加載完之前就放於內存中,且只加載這一次,new類對象時是不會再次執行的了。
30 static
31 {
32 System.out.print("e");
33 inner.innerMethed("f"); // 靜態初始化塊,整個靜態的都是在加載之前就做好初始化。
34 new inner();
35 }
36
37
38 private int i=say("g"); // 在new時,先於構造但是后於初始化塊的一個實例變量,也即:實例對象:(實例初始化塊,實例變量)--->構造方法
39 private inner tt= new inner(); // 整個是在類加載ClassLoader之后初始化變量時,才做。每個new對象都會執行上面這個步驟
40 private innerOther ttt= new innerOther();
41
42
43
44 static class inner{
45
46
47 public inner(){
48 System.out.print("h");
49 }
50
51
52 public static void innerMethed(String s){
53 System.out.print(s);
54 }
55
56
57
58 {
59 System.out.print("i");
60 }
61
62
63
64 static
65 {
66 System.out.print("j");
67 }
68
69
70 }
71
72
73
74 class innerOther{
75
76
77 public innerOther(){
78 System.out.print("k");
79 }
80
81
82 {
83 System.out.print("l");
84 }
85
86
87
88 }
89
90
91
92 public static void main(String[] args) {
93 System.out.print("m");
94 Outer outer = new Outer();
95 System.out.print("n");
96
97 // 總結就是:類對象,變量的加載順序
98 // 在整個類加載完成之后,就初始化靜態(靜態變量,靜態塊),其中可以new對象,別的類調用靜態方法。靜態的只做這一次,然后類加載器ClassLoader就把類Class返還,執行main方法。
99 // 在new類對象時,執行順序是:(實例初始化塊,實例變量)-->構造方法。每new一個對象時,都會執行這個步驟。
100
101 // (靜態變量、靜態初始化塊)—— 》 (變量、初始化塊)—— 》 構造函數,()中的內容是按照代碼前后循序執行
102
103 // 當有繼承關系時,還是基於這個原理,如new Son();
104 // 父類的靜態(塊和變量)-->子類的靜態(塊和變量)-->父類的(塊和變量+構造方法)-->子類的(塊和變量+構造方法)
105 //
106 }
107 }
//類的是通過:裝載,連接,初始化
//java程序在執行過程中,類,對象以及它們成員加載、初始化的順序如下:
//1、首先加載要創建對象的類及其直接與間接父類。
//2、在類被加載的"同時"會將靜態成員進行加載,主要包括靜態成員變量的初始化,靜態語句塊的執行,在加載時按代碼的先后順序進行。 <clinit>
//3、需要的類加載完成后,開始創建對象,首先會加載非靜態的成員,主要包括非靜態成員變量的初始化,非靜態語句塊的執行,在加載時按代碼的先后順序進行。
//4、最后執行構造器,構造器執行完畢,對象生成。