构造块:在类成员变量区域用大括号括起来的代码,和方法相比没有修饰,没有返回,没有参数;
静态块:在构造块前加 static修饰
静态代码块:静态块+静态变量
非静态代码块:普通类成员变量+构造块
程序执行顺序:静态代码块->非静态代码块->类构造方法
1 public class Test {
2
3 static {
4 System.out.println("静态块");
5 }
6
7 {
8 System.out.println("构造块");
9 }
10
11 public Test(String str) {
12 System.out.println("构造函数"+str);
13 }
14
15 public static void main(String[] strings) {
16 Test t1 = new Test("t1");
17 Test t2 = new Test("t2");
18 }
19 }
执行结果为:
静态块
构造块
构造函数t1
构造块
构造函数t2
也就是说静态块会先于main函数执行;(没有main函数,程序还能执行吗?)
然后是main里面实例化对象,这时程序会跳到构造函数那一行,但不会直接执行构造函数中的内容,而是开始执行非静态代码块,等执行完这一部分之后再执行构造函数;
还有一点,从结果可以看出静态代码块只执行了一次。
有一种特殊情况,静态对象
在静态变量中new一个自身的对象
1 public class Test {
2
3 public static int k = 0;
4
5 public static Test t1 = new Test("t1");
6
7 public static int i = print("i");
8 public static int n = 99;
9
10 public int y=0;
11 static {
12 System.out.println("静态块");
13 }
14
15 public int y1=0;
16 public int y2=0;
17 public int y3=0;
18 public int y4=0;
19 public int j1 = print("j1");
20 {
21 System.out.println("构造块");
22 }
23
24
25 public Test(String str) {
26 System.out.println((++k) + ":" + str + " i=" + i + " n=" + n);
27 ++i;
28 ++n;
29 }
30
31 public static int print(String str) {
32 System.out.println((++k) + ":" + str + " i=" + i +" n=" + n);
33 ++n;
34 return ++i;
35 }
36
37 public static void main(String[] strings) {
38 Test t = new Test("init");
39 }
40
41 }
执行结果:
1:j1 i=0 n=0
构造块
2:t1 i=1 n=1
3:i i=2 n=2
静态块
4:j1 i=3 n=99
构造块
5:init i=4 n=100
从结果中我们发现,”静态块“居然不是第一个输出,debug调试后发现执行顺序是
1.public static int k = 0;
2.public static Test t1 = new Test("t1");
3.public Test(String str) //构造函数,但不执行里面的内容
4.public int y=0;
5.public int y1=0;
...
6.public int j1 = print("j1");
7.System.out.println("构造块");
8.public Test(String str) { ...}//构造函数里面的内容
确实是先执行了静态块,不过遇到静态变量时
程序会先跳到构造函数,但不执行里面的内容,然后开始执行非静态代码块(注意 public int y=0; 也是非静态代码块中的一份子)
然后执行构造函数
所以才会出现了这样的结果
以下是执行顺序,抽之间重新画一下
以下是阿里的一到面试题
比上面的多一个地方
静态块和构造块中内容都是输出,在构造块之后又new了一个t2对象
不过没有关系,用上面的分析依然可以得到结果
1 public class Test {
2
3 public static int k = 0;
4
5 public static Test t1 = new Test("t1");
6
7 public static int i = print("i");
8 public static int n = 99;
9
10 static {
11 print("静态块");
12 }
13
14 public int j1 = print("j1");
15 {
16 print("构造块");
17 }
18
19 public int j = print("j");
20
21 public static Test t2 = new Test("t2");
22
23 public Test(String str) {
24 System.out.println((++k) + ":" + str + " i=" + i + " n=" + n);
25 ++i;
26 ++n;
27 }
28
29 public static int print(String str) {
30 System.out.println((++k) + ":" + str + " i=" + i +" n=" + n);
31 ++n;
32 return ++i;
33 }
34
35 public static void main(String[] strings) {
36 Test t = new Test("init");
37 }
38 }
结果为:
1:j1 i=0 n=0
2:构造块 i=1 n=1
3:j i=2 n=2
4:t1 i=3 n=3
5:i i=4 n=4
6:静态块 i=5 n=99
7:j1 i=6 n=100
8:构造块 i=7 n=101
9:j i=8 n=102
10:t2 i=9 n=103
11:j1 i=10 n=104
12:构造块 i=11 n=105
13:j i=12 n=106
14:init i=13 n=107
第一行输入代码为
public int j = print("j");
-->System.out.println((++k) + ":" + str + " i=" + i +" n=" + n);
这里输出了i的值为0,根据上面的分析,变量i的初始化即
public static int i = print("i");
这一行还没有执行到,那为什么不报错呢?