Java静态代码块与静态变量定义的执行顺序

总结:按顺序执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class StaticTest {
public static String API_DEV_BASE_URL = "http://192.168.10.10/";
public static String API_PRO_BASE_URL = "http://api.appblog.cn/";
public static String API_BASE_URL = API_PRO_BASE_URL;
public static String LOG_REPORT_URL = API_BASE_URL + "log";

static {
API_BASE_URL = API_DEV_BASE_URL;
}

public static void main(String[] args) {
System.out.println(LOG_REPORT_URL);
}
}

输出结果:

1
http://api.appblog.cn/log
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class StaticTest {
public static String API_DEV_BASE_URL = "http://192.168.10.10/";
public static String API_PRO_BASE_URL = "http://api.appblog.cn/";
public static String API_BASE_URL = API_PRO_BASE_URL;

static {
API_BASE_URL = API_DEV_BASE_URL;
}

public static String LOG_REPORT_URL = API_BASE_URL + "log";

public static void main(String[] args) {
System.out.println(LOG_REPORT_URL);
}
}

输出结果:

1
http://192.168.10.10/log

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :