关于Java构造器初始化
1、初始化顺序
1 package com.demo.initialization; 2 3 class Window { 4 Window(int marker) { 5 System.out.println("Window(" + marker + ")"); 6 } 7 } 8 9 /** 10 * 变量定义初始化,会在任何方法之前 11 * w1、w2、w3为变量,会在构造器之前初始化 12 * @author 小小树 13 */ 14 class House { 15 Window w1 = new Window(1); 16 House() { 17 System.out.println("House()"); 18 Window w3 = new Window(3); 19 } 20 Window w2 = new Window(2); 21 void f() { 22 System.out.println("f()"); 23 } 24 Window w3 = new Window(3); 25 } 26 27 /** 28 * 演示类内部初始化顺序 29 * @author 小小树 30 * 创建是时间: 2020年10月6日 31 */ 32 public class OrderOfInitialization { 33 34 public static void main(String[] args) { 35 // TODO Auto-generated method stub 36 House h = new House(); 37 h.f(); 38 } 39 40 }
结果如下:
Window(1) Window(2) Window(3) House() Window(3) f()
总结:在类的内部,变量定义的先后顺序决定了初始化的顺序,即便是构造器,也会在它之前。
2、静态数据的初始化
1 package com.demo.initialization; 2 3 class Bowl { 4 Bowl(int marker){ 5 System.out.println("Bowl(" + + marker + ")"); 6 } 7 void f1(int marker) { 8 System.out.println("f1(" + marker + ")"); 9 } 10 } 11 12 class Table { 13 static Bowl bowl = new Bowl(1); 14 Table() { 15 System.out.println("Table()"); 16 bowl2.f1(1); 17 } 18 void f2(int marker) { 19 System.out.println("f2(" + marker + ")"); 20 } 21 static Bowl bowl2 = new Bowl(2); 22 } 23 24 class Cupboard { 25 Bowl bowl3 = new Bowl(3); 26 static Bowl bowl4 = new Bowl(4); 27 Cupboard() { 28 System.out.println("Cupboard()"); 29 bowl4.f1(2); 30 } 31 void f3(int marker) { 32 System.out.println("f3(" + marker + ")"); 33 } 34 static Bowl bowl5 = new Bowl(5); 35 } 36 37 /** 38 * 静态数据初始化 39 * @author 小小树 40 * @date 2020/10/6 41 */ 42 public class StaticInitialization { 43 public static void main(String[] args) { 44 // TODO Auto-generated method stub 45 System.out.println("Creating new Cupboard() in main"); 46 new Cupboard(); 47 System.out.println("Creating new Cupboard() in main"); 48 new Cupboard(); 49 table.f2(1); 50 cupboard.f3(1); 51 } 52 /** 53 * 程序优先执行static成员,顺序执行 table、cupboard两个静态对象,再进入main方法 54 * static对象只会创建一次,没有被执行的必要则不会创建static对象 55 */ 56 static Table table = new Table(); 57 static Cupboard cupboard = new Cupboard(); 58 }
结果如下:
Bowl(1) Bowl(2) Table() f1(1) Bowl(4) Bowl(5) Bowl(3) Cupboard() f1(2) Creating new Cupboard() in main Bowl(3) Cupboard() f1(2) Creating new Cupboard() in main Bowl(3) Cupboard() f1(2) f2(1) f3(1)
总结:静态数据只会占用一份存储区域,static关键字不能应用于局部变量,只能作用于域。补充:注意看代码注释。
3、显式的静态初始化
1 package com.demo.initialization; 2 3 class Cup { 4 Cup(int marker) { 5 System.out.println("Cup("+ marker + ")"); 6 } 7 void f(int marker) { 8 System.out.println("f(" + marker + ")"); 9 } 10 } 11 12 /** 13 * static语句块只会执行一次 14 * @author 小小树 15 * @date 2020/10/6 16 */ 17 class Cups { 18 static Cup cup1; 19 static Cup cup2; 20 static { 21 cup1 = new Cup(1); 22 cup2 = new Cup(2); 23 } 24 Cups() { 25 System.out.println("Cups()"); 26 } 27 } 28 29 /** 30 * 显示的静态初始化 31 * @author 小小树 32 * @date 2020/10/6 33 */ 34 public class ExplicitStatic { 35 public static void main(String[] args) { 36 // TODO Auto-generated method stub 37 System.out.println("Inside main()"); 38 Cups.cup1.f(99); 39 } 40 static Cups cups1 = new Cups(); 41 static Cups cups2 = new Cups(); 42 }
结果如下:
Cup(1) Cup(2) Cups() Cups() Inside main() f(99)
总结:static关键字后面的代码只会执行一次。
4、非静态实例初始化
1 package com.demo.initialization; 2 class Mug { 3 Mug(int marker) { 4 System.out.println("Mug(" + marker + ")"); 5 } 6 void f(int marker) { 7 System.out.println("f("+ marker +")"); 8 } 9 } 10 11 /** 12 * 非静态实力初始化 13 * @author 小小树 14 * @date 2020/10/7 15 */ 16 public class Mugs { 17 Mug mug1; 18 Mug mug2; 19 { 20 mug1 = new Mug(1); 21 mug2 = new Mug(2); 22 System.out.println("mug1 & mug2 initialized"); 23 } 24 /** 25 * 测试构造器初始化是否比类成员初始化快 26 */ 27 Mugs() { 28 System.out.println("Mugs()"); 29 } 30 public static void main(String[] args) { 31 System.out.println("Insie main"); 32 new Mugs(); 33 System.out.println("new Mugs() completed"); 34 new Mug(1); 35 System.out.println("new Mug(1) completed"); 36 // 再次执行Mug() 观察是否再初始化mug1、mug2 37 new Mugs(); 38 System.out.println("new Mugs() completed"); 39 } 40 }
结果如下:
Insie main Mug(1) Mug(2) mug1 & mug2 initialized Mugs() new Mugs() completed Mug(1) new Mug(1) completed Mug(1) Mug(2) mug1 & mug2 initialized Mugs() new Mugs() completed

浙公网安备 33010602011771号