Constructor initialization and constructor Initialization
1. Initialization order
Within the class, the order in which variables are defined determines the initialization order. When real-time variables are scattered between method definitions, they will still be in any method (including constructors) it is initialized before being called.
1 class Window { 2 Window(int maker) { 3 System.out.println("Window(" + maker + ")"); 4 } 5 } 6 7 class House { 8 Window window1 = new Window(1); 9 10 House() {11 System.out.println("House()");12 w3 = new Window(33);13 }14 15 Window window2 = new Window(2);16 17 void f() {18 System.out.println("f()");19 }20 21 Window w3 = new Window(3);22 23 }24 25 public class OrderOfInitialization {26 public static void main(String[] args) {27 House h = new House();28 h.f();29 }30 }
Window(1)Window(2)Window(3)House()Window(33)f()
The output shows that the reference w3 is initialized twice: one before the constructor is called, and the other before the call. (The object referenced for the first time is discarded and recycled as garbage ).
2. Static Data Initialization
No matter how many objects are created, static data occupies only one storage area. The static keyword cannot be applied to local variables, so it can only apply to fields.
1 class Bowl { 2 Bowl(int maker) { 3 System.out.println("Bowl(" + maker + ")"); 4 } 5 6 void f1(int maker) { 7 System.out.println("f1(" + maker + ")"); 8 } 9 }10 11 class Table {12 static Bowl bowl1 = new Bowl(1);13 14 Table() {15 System.out.println("Table()");16 bowl2.f1(1);17 }18 19 void f2(int maker) {20 System.out.println("f2(" + maker + ")");21 }22 23 static Bowl bowl2 = new Bowl(2);24 }25 26 class Cupboard {27 Bowl bowl3 = new Bowl(3);28 static Bowl bowl4 = new Bowl(4);29 30 Cupboard() {31 System.out.println("CupBoard()");32 bowl4.f1(2);33 }34 35 void f3(int maker) {36 System.out.println("f3(" + maker + ")");37 }38 39 static Bowl bowl5 = new Bowl(5);40 }41 42 public class StaticInitialization {43 public static void main(String[] args) {44 System.out.println("created new Cupboard() in main");45 new Cupboard();46 System.out.println("created new Cupboard in main");47 new Cupboard();48 table.f2(1);49 cupboard.f3(1);50 }51 52 static Table table = new Table();53 static Cupboard cupboard = new Cupboard();54 }
Bowl(1)Bowl(2)Table()f1(1)Bowl(4)Bowl(5)Bowl(3)CupBoard()f1(2)created new Cupboard() in mainBowl(3)CupBoard()f1(2)created new Cupboard in mainBowl(3)CupBoard()f1(2)f2(1)f3(1)
The output shows that static Initialization is only performed when necessary. If you do not create a Table object or reference Table. b1 or Table. b2, static Bowl b1 and b2 will never be created. They are initialized only when the first Table object is created (or accessed for the first time. After that, the static object will not be initialized again.
3. Non-static instance Initialization
The instance initialization clause is executed before the two constructors.
1 class Mug { 2 Mug(int maker) { 3 System.out.println("Mug(" + maker + ")"); 4 } 5 6 void f(int maker) { 7 System.out.println("f(" + maker + ")"); 8 } 9 }10 11 public class Mugs {12 Mug mug1;13 Mug mug2;14 {15 mug1 = new Mug(1);16 mug2 = new Mug(2);17 System.out.println("mug1 & mug2 initialized");18 }19 20 Mugs() {21 System.out.println("Mugs()");22 }23 24 Mugs(int maker) {25 System.out.println("Mugs(int)");26 }27 28 public static void main(String[] args) {29 System.out.println("Inside main()");30 new Mugs();31 System.out.println("new Mugs() complete");32 new Mugs(1);33 System.out.println("new Mugs(1) complete");34 }35 }
Inside main()Mug(1)Mug(2)mug1 & mug2 initializedMugs()new Mugs() completeMug(1)Mug(2)mug1 & mug2 initializedMugs(int)new Mugs(1) complete