(静态)初始化块、(类)成员变量、构造函数的初始化顺序

本文转自此处

源代码:

public class Teststaticblock 
{ 
	public Teststaticblock() 
	{ 
  		this("second"); 
  		System.out.println("begin constructor"); 
  		System.out.println(s_a); //111
  		System.out.println(s_b); //222
  		System.out.println(c); //333
  		System.out.println(d); //d的值为什么是4,却不是444??
				       //因为程序会按照初始化块和实例Field生命语句的先后顺序来执行,
				       //先赋给d的值444会被后赋给d的值4覆盖掉,所以d的最终值为4.
		//  this("second");//call to this must be first statement in constructor 
  		s_a=1111; 
  		s_b=2222; 
  		c=3333; 
  		d=4444; 
  		System.out.println(s_a); //1111
  		System.out.println(s_b); //2222
  		System.out.println(c); //3333
  		System.out.println(d); //4444
  		System.out.println("end constructor"); 
	} 
	public Teststaticblock(String s) 
	{ 
  		System.out.println("begin second constructor"); 
  		System.out.println("end second constructor"); 
	} 
	public static void main(String args[]) 
	{ 
  		System.out.println("begin main"); 
  		System.out.println(s_a); 
 		System.out.println(s_b); //s_b的值为什么是2,却不是22??
					 //因为程序会按照静态初始化块和静态Field声明语句的先后顺序来执行,先赋给s_b的值22
					 //会被后赋给s_b的值2覆盖掉,所以s_b最终的值为2.
		//  System.out.println(c);
                //non-static variable c cannot be referenced from a static context 
		//  System.out.println(d);
                //non-static variable c cannot be referenced from a static context 
  		s_a=11111; 
  		s_b=22222; 
		//  c=33333;//non-static variable c cannot be referenced from a static context 
		//  d=44444;//non-static variable c cannot be referenced from a static context 
 		System.out.println(s_a); //11111
  		System.out.println(s_b); //22222
		//  System.out.println(c);
                //non-static variable c cannot be referenced from a static context 
		//  System.out.println(d);
                //non-static variable c cannot be referenced from a static context 
  		System.out.println("before new class object"); 
  		Teststaticblock t = new Teststaticblock(); 
  		System.out.println("end new class object"); 
  		System.out.println(s_a);//1111 
  		System.out.println(s_b); //2222
		//  System.out.println(c);
                //non-static variable c cannot be referenced from a static context 
		//  System.out.println(d);
                //non-static variable c cannot be referenced from a static context 
  		s_a=111111; 
  		s_b=222222; 
		//  c=333333;//non-static variable c cannot be referenced from a static context 
		//  d=444444;//non-static variable c cannot be referenced from a static context 
  		System.out.println(s_a); //111111
  		System.out.println(s_b); //222222
		//  System.out.println(c);
                //non-static variable c cannot be referenced from a static context 
		//  System.out.println(d);
                //non-static variable c cannot be referenced from a static context 
  		System.out.println("end main"); 
	} 

	static int s_a=1; 
	int c=3; 
	{ 
  		System.out.println("begin block"); 
  		System.out.println(s_a); //11111
  		System.out.println(s_b); //22222
  		System.out.println(c); //3
		//  System.out.println(d);//illegal forward reference 
  		s_a=111; 
  		s_b=222; 
  		c=333; 
  		d=444; 
  		System.out.println(s_a); //111
  		System.out.println(s_b); //222
  		System.out.println(c); //333
		//  System.out.println(d);//illegal forward reference 
  		System.out.println("end block"); 
	} 
	static 
	{ 
  		System.out.println("begin static block"); //begin static block
  		System.out.println(s_a);//1
		//为什么不能输出s_b的值,却可以给s_b赋值??
		//  System.out.println(s_b);//illegal forward reference 
		//  System.out.println(c);
                //non-static variable c cannot be referenced from a static context 
		//  System.out.println(d);
                //non-static variable c cannot be referenced from a static context 
  		s_a=11; 
  		s_b=22; 
  		System.out.println(s_a); //11
		//  System.out.println(s_b);//illegal forward reference 
		//  System.out.println(c);
                //non-static variable c cannot be referenced from a static context 
		//  System.out.println(d);
                //non-static variable c cannot be referenced from a static context 
  		System.out.println("end static block"); 
	} 
	int d=4; 
	static int s_b=2; 

} 

 运行结果:

/*
begin static block 
1 
11 
end static block 
begin main 
11 
2 
11111 
22222 
before new class object 
begin block 
11111 
22222 
3 
111 
222 
333 
end block 
begin second constructor 
end second constructor 
begin constructor 
111 
222 
333 
4 
1111 
2222 
3333 
4444 
end constructor 
end new class object 
1111 
2222 
111111 
222222 
end main 
*/

 结果分析:

1、在类第一次加载时候,会执行静态成员变量(静态Field)初始化语句和静态初始化块(用static{}包含的部分)。

注意:

a、不管静态成员变量声明语句的实际位置在哪儿,当第一次加载类的时候都会首先对它初始化为缺省值(0,false,null等)。

b、即使静态成员变量声明中使用了显式初始化语句(比如:int x=3),第一次加载类的时候也会先把它初始化为缺省值(此时x为0),然后再按照下面说的要点c来执行赋值语句(x=3)。

c、对于静态成员变量的显式初始化语句和静态初始化块,按照在类中代码出现的先后顺序执行。 

     因此,在上面的例子程序中,我们看到 

      static int s_a=1; 

      static 

      { 

         s_a=11; 

         s_b=22; 

       } 

     static int s_b=2; 

对s_a,s_b会有不同的效果。

类加载时候,s_a,s_b都被初始化为0,然后由于依照代码顺序执行了s_a=1;s_a=11;s_b=22;s_b=2;

结果s_a、s_b分别变成了11和2。 

 

2、当构造类实例时候,会先对实例成员变量初始化为缺省值,然后执行初始化块(用{}括起来的部分),然后执行构造方法。其中:

a、如同1中一样,如果有实例成员变量的显式初始化语句,程序仍然是先将该成员变量初始化为缺省值,然后按照代码在类中出现的先后顺序执行初始化语句或者初始化块。

     如果初始化块位置在初始化语句前面,即使它改变了该成员变量的值,也会被随后执行的初始化语句改回去。 

b、在进入构造方法后,如果构造方法第一句是使用this(...)调用另一构造方法的话,则先执行另一构造方法,然后再执行本构造方法的方法体。这种用法必须让this(...)位于第一句。

猜你喜欢

转载自guoying252166655.iteye.com/blog/2101457