Java静态代码块、构造器、静态主方法以及继承后父类代码块的运行顺序

以下代码转自CSDN网Flaker的Blog,链接:http://blog.csdn.net/flaker/archive/2009/10/10/4649367.aspx

 1 public class Child extends Super{
 2     static {
 3         System.out.println("Child's static");
 4     }
 5     Child() {
 6         System.out.println("Child");
 7     }
 8     public static void main(String[] args) {
 9         System.out.println("main");
10         Child c = new Child();
11     }
12 }  
13 
14 public class Super {
15     static {
16         System.out.println("Super's static");
17     }
18     Super() {
19         System.out.println("Super");
20     }
21 }

执行结果如下:
1 Super's static  
2 Child's static  
3 main  
4 Super  
5 Child 

分析:
按执行先后顺序排列
1、父类的静态代码块;
2、子类的静态代码块;
3、静态main方法;
4、实例化父类;
5、实例化子类。

posted on 2009-10-10 15:13 Rick Qin 阅读(1007) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

统计

留言簿(2)

随笔分类(3)

随笔档案(6)

文章分类

文章档案(3)

搜索

最新随笔

最新评论

阅读排行榜

评论排行榜