论坛首页 入门技术论坛

(四): Java中静态代码块及对象的初始化顺序

浏览 3812 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (8) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-02-26   最后修改:2009-03-03
class Parent{
	static String name = "hello";
	{
		System.out.println("parent  block");
	}
	static {
		System.out.println("parent static block");
	}
	public Parent(){
		System.out.println("parent constructor");
	}
}

class Child extends Parent{
	static String childName = "hello";
	{
		System.out.println("child  block");
	}
	static {
		System.out.println("child static block");
	}
	public Child(){
		System.out.println("child constructor");
	}
}

public class StaticIniBlockOrderTest {

	public static void main(String[] args) {
        new Child();//语句(*)
	}
}

问题:当执行完语句(*)时,打印结果是什么顺序?为什么?
解答:当执行完语句(*)时,打印结果是这样一个顺序:parent static block,child static block,parent  block,parent constructor,child  block,child constructor。
分析:当执行new Child()时,它首先去看父类里面有没有静态代码块,如果有,它先去执行父类里面静态代码块里面的内容,当父类的静态代码块里面的内容执行完毕之后,接着去执行子类(自己这个类)里面的静态代码块,当子类的静态代码块执行完毕之后,它接着又去看父类有没有非静态代码块,如果有就执行父类的非静态代码块,父类的非静态代码块执行完毕,接着执行父类的构造方法;父类的构造方法执行完毕之后,它接着去看子类有没有非静态代码块,如果有就执行子类的非静态代码块。子类的非静态代码块执行完毕再去执行子类的构造方法,这个就是一个对象的初始化顺序。

总结:对象的初始化顺序:首先执行父类静态的内容,父类静态的内容执行完毕后,接着去执行子类的静态的内容,当子类的静态内容执行完毕之后,再去看父类有没有非静态代码块,如果有就执行父类的非静态代码块,父类的非静态代码块执行完毕,接着执行父类的构造方法;父类的构造方法执行完毕之后,它接着去看子类有没有非静态代码块,如果有就执行子类的非静态代码块。子类的非静态代码块执行完毕再去执行子类的构造方法。总之一句话,静态代码块内容先执行,接着执行父类非静态代码块和构造方法,然后执行子类非静态代码块和构造方法。

注意:子类的构造方法,不管这个构造方法带不带参数,默认的它都会先去寻找父类的不带参数的构造方法。如果父类没有不带参数的构造方法,那么子类必须用supper关键子来调用父类带参数的构造方法,否则编译不能通过。
   发表时间:2009-02-27   最后修改:2009-02-27
 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();//语句(*)  
     }  
 } 

好象这样更加全!
0 请登录后投票
   发表时间:2009-02-27  
stanfine 写道
 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();//语句(*)  
     }  
 } 

好象这样更加全!


不错,不错.
0 请登录后投票
   发表时间:2009-02-27  
不错不错,欢迎大家批评指正!
0 请登录后投票
   发表时间:2009-02-27  
stanfine 写道
 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();//语句(*)  
     }  
 } 

好象这样更加全!



原来见过类似题目。。 
{
         System.out.println("child block");
     }   
这样的代码是几时初始化的呢?有些忘了。
0 请登录后投票
   发表时间:2009-02-27  
{ 
         System.out.println("child block"); 
     } 
  
这样的代码是在静态代码块初始化之后,构造方法初始化之前初始化的!
0 请登录后投票
   发表时间:2009-02-28  
想起来了。。看了以前总结的笔记:父类Static->子类static->缺省{}->构造函数

parent static block
child static block
parent  block
child  block
parent constructor
child  block,child constructor

顺序该是这样的..对吧
0 请登录后投票
   发表时间:2009-02-28  
顺序应该是这样的:父类Static->子类static->父类缺省{}->父类构造函数->子类缺省{}->子类构造函数
所以上面的例子打印顺序应该是这样的:
parent static block 父类Static
child static block 子类static
parent  block 父类缺省{}
parent constructor 父类构造函数
child  block子类缺省{}
child constructor子类构造函数
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics