Java和Android中,代码块、static静态代码块的执行顺序
程序员文章站
2022-12-03 13:16:07
Java和Android中,代码块、static静态代码块的执行顺序有没有什么区别呢。
Java
先来个简单的例子
Main.java:...
Java和Android中,代码块、static静态代码块的执行顺序有没有什么区别呢。
Java
先来个简单的例子
Main.java:
1234567891011121314151617 |
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变量和代码块优先执行
我们再来做一个更复杂的实验
123456789101112131415161718192021222324252627282930313233343536 |
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
Android
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
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加载(从结果来看,是在类被使用的时候才会加载)的时候执行。
上一篇: ISIS OSPF双向重发布