java 程序的执行程序
- 静态语句块最先被执行
- 正常语句
- 执行块语句
- 执行构造函数[先父类再子类]
- 如果有父类,则先按照顺序执行父类
以下代码可以解释
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
| package ExecuteOrder;
class A { public A() { System.out.print("Class A here\n"); }
{ System.out.print("block here\n"); }
static { System.out.print("static block A here\n"); } }
public class ExecuteOrder extends A {
public ExecuteOrder() { System.out.print("Class B here\n"); }
{ System.out.print("block here\n"); }
static { System.out.print("static block B here\n"); }
public static void main(String[] args) { System.out.print("main begin\n"); new ExecuteOrder(); System.out.print("main end\n"); } }
static block A here static block B here main begin block here Class A here block here Class B here main end
|
参考博客
http://java-mzd.iteye.com/blog/838683
http://www.cnblogs.com/lanxuezaipiao/p/3371224.html