Java和Android中,代码块、static静态代码块的执行顺序

Java和Android中,代码块、static静态代码块的执行顺序有没有什么区别呢。

Java

先来个简单的例子

Main.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Main {

static int a = 1;

static {
System.out.println(a);
}

static {
a = 2;
}

public static void main(String[] args) {
System.out.println("Hello World!");
System.out.println(a);
}
}

输出为

1
Hello World!
2

尝试交换static变量声明和static代码块后,编译报错”非法前向引用”。
交互两个代码块位置后输出

2
Hello World!
2

可见static修饰的,是从上向下,依次执行的。static变量和代码块优先执行

我们再来做一个更复杂的实验

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
public class Main {

static int a = 1;

static {
System.out.println(a);
}

static {
a = 2;
}

public static void main(String[] args) {
System.out.println("Hello World!");
Yo.test();
Yo yooo = new Yo();
yooo.test();
}
}

public class Yo {
static String name = "mark";

static {
System.out.println(name);
}

public Yo() {
super();
System.out.println("constructor");
}

static public void test() {
System.out.println("hello " + name);
}
}

输出

1
Hello World!
mark
hello mark
constructor
hello mark

Yo的执行符合上面的结论,执行了static代码块和静态方法test,构造Yo实例后,没有重复执行static代码块。

Android

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
public class TestApplication extends BaseApplication {
static {
Log.d("test", "Application 1123");
}

static int a = 1;

static {
Log.d("test", "Application " + a);
HomeActivity.test();
}

static {
a = 2;
}

@Override
protected void attachBaseContext(Context base) {
Log.d("test", "Application hello " + a);
}
}

public class SplashActivity extends BaseActivity {

static {
Log.d("test", "SplashActivity static code block");
}

static int a = 1;

static {
Log.d("test", "SplashActivity " + a);
}

static {
a = 2;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("test", "SplashActivity onCreate " + a);
// 省略,会去start HomeActivity
}
}

public class HomeActivity extends BaseActivity {

public static final String TAG = "HomeActivity";

static {
Log.d("test", "HomeActivity 1123");
}

public static void test() {
Log.d("test", "HomeActivity");
}
}

输出结果为

D/test﹕ Application 1123
D/test﹕ Application 1
D/test﹕ HomeActivity 1123
D/test﹕ HomeActivity
D/test﹕ Application hello 2
D/test﹕ SplashActivity static code block
D/test﹕ SplashActivity 1
D/test﹕ SplashActivity onCreate 2

从结果上看java并没有什么区别

同样是先执行依次static,入口外其他类的static初始化在类被JVM加载(从结果来看,是在类被使用的时候才会加载)的时候执行。

Mark Zhai (翟一帆) wechat
欢迎您扫一扫上面的微信公众号,订阅我们的公众号!
坚持原创技术分享,您的支持将鼓励我继续创作!