Chinaunix首页 | 论坛 | 博客
  • 博客访问: 845803
  • 博文数量: 221
  • 博客积分: 10033
  • 博客等级: 上将
  • 技术积分: 2325
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-06 22:00
文章分类

全部博文(221)

文章存档

2010年(8)

2009年(1)

2008年(69)

2007年(63)

2006年(80)

我的朋友

分类: Java

2010-07-28 11:14:43

顺序应该是这样的:父类Static->子类static->父类缺省{}->父类构造函数->子类缺省{}->子类构造 函数
所以上面的例子打印顺序应该是这样的:
parent static block 父类Static
child static block 子类static
parent  block 父类缺省{}
parent constructor 父类构造函数
child  block子类缺省{}
child constructor子类构造函数

class Parent{
     static String name = "hello";
     static {
         System.out.println("parent static block");
     }
     {
         System.out.println("parent block");
     }
     public Parent(){
         System.out.println("parent constructor");
     }
 }
   
 class Child extends Parent{
     static String childName = "hello";
     static {
         System.out.println("child static block");
     }
     {
         System.out.println("child block");
     }
     public Child(){
         System.out.println("child constructor");
     }
 }
   
 public class StaticIniBlockOrderTest {
   
     public static void main(String[] args) {
         new Child();//语句(*)

     }
 }


阅读(1112) | 评论(0) | 转发(0) |
0

上一篇:Java Enum学习

下一篇:Eclipse快捷键

给主人留下些什么吧!~~