Java中程序初始化的顺序

1,在一个类的内部(不考虑它是另一个类的派生类):很多人认为,类的成员变量是在构造方法调用之后再初始化的,先不考虑这种观点的正确性,先看一下下面的代码:

[java] view plaincopy
 
 
  1. class Test01...{  
  2.     public Test01(int i)...{  
  3.         System.out.println("Test01 of constractor : " + i);  
  4.     }  
  5. }  
  6. public class Test02 ...{  
  7.     private Test01 t1 = new Test01(1);  
  8.     private int n = 10;  
  9.       
  10.     public Test02()...{  
  11.         System.out.println("Test02 of constructor : " + n);  
  12.     }  
  13.     private Test01 t2 = new Test01(2);  
  14.     public static void main(String[] args) ...{  
  15.         Test02 test = new Test02();  
  16.     }  
  17.   
  18. }  
  19. 输出的结果为:  
  20. Test01 of constractor : 1  
  21. Test01 of constractor : 2  
  22. Test02 of constructor : 10  

通过输出,可见当生成Test02的实例test时,它并不是首先调用其构造方法而是先是成员变量的初始化,而且成员的初始化的顺序以成员变量的定义顺序有关,先定义的先初始化,初始化后再调用构造方法。其实成员变量的初始化,在类的所有方法调用之前进行,包括构造方法
当类中有Static 修饰的成员呢?测试下面一段代码:

[java] view plaincopy
 
 
  1. public class Test03 ...{  
  2.     private int i1 = printCommon();  
  3.     private static int i2 = printStatic();  
  4.       
  5.     public Test03()...{  
  6.           
  7.     }  
  8.     public static int printCommon()...{  
  9.         System.out.println("i1 is init!");  
  10.         return 1;  
  11.     }  
  12.     public static int printStatic()...{  
  13.         System.out.println("i2 is init!");  
  14.         return 2;  
  15.     }  
  16.     public static void main(String[] args) ...{  
  17.         Test03 t = new Test03();  
  18.     }  
  19. }  
  20.   
  21. 输出结果为:  
  22. i2 is init!  
  23. i1 is init!  

可见static的成员比普通的成员变量先初始化。
我们都知道,如果一个类的成员变量没有在定义时,系统会给予系统默认的值,有=号的就直接给予右值,系统在给予初值和=号给予值这2中方式,在执行时间上有先后吗?为了测试,我编写了如下代码:

[java] view plaincopy
 
 
  1. public class Test04 ...{  
  2.     private static Test04 t1 = new Test04();  
  3.     private static int i1;  
  4.     private static int i2 = 2;  
  5.       
  6.     public Test04()...{  
  7.         i1++;  
  8.         i2++;  
  9.     }  
  10.       
  11.     public static void main(String[] args) ...{  
  12.         Test04 t2 = new Test04();  
  13.         System.out.println("t2.i1 = " + t2.i1);  
  14.         System.out.println("t2.i2 = " + t2.i2);  
  15.     }  
  16. }  
  17. 我们先预计一下输出,可能有几种答案:2和3,3和3,2和2  
  18. 执行代码后:  
  19. t2.i1 = 2  
  20. t2.i2 = 3  

为什么是2和3呢?其实代码的执行顺序是这样的:首先执行给t1,i1,i2分别给予初始值null,0,0,再执行
Test04 t1 =new Test04(),这样i1++,i2++被执行,i1,i2都变为1,执行完毕后接着执行int i1; i1,i2的值仍然是1,1,当执行int i2 = 2时i2被赋予了值,即i1 = 1,i2=2;再执行Test04 t2 = new Test04(),i1,i2再执行++,此时i1 =2,i2 =3,输出i1,i2,结果就是:t2.i1 = 2,t2.i2 = 3。 通过上面的代码我们可以认为系统默认值的给予比通过等号的赋予先执行。
2,一个类还有上层的类,即父类:
      当生成一个子类时,大家到知道会调用父类的构造方法。如果子类和父类中都有Static的成员变量呢,其实我们在深入分析一个类的内部初始化后,对于存在父类的类的初始化其实原理都一样,具体以下面的代码为例:

[java] view plaincopy
 
 
  1. class SuperClass ...{  
  2.     static...{  
  3.         System.out.println("SuperClass of static block");  
  4.     }  
  5.       
  6.     public SuperClass()...{  
  7.         System.out.println("SuperClass of constracutor");  
  8.     }  
  9. }  
  10.   
  11. public class SubClass extends SuperClass...{  
  12.     static...{  
  13.         System.out.println("SubClass of static block");  
  14.     }  
  15.       
  16.     public SubClass()...{  
  17.         System.out.println("SubClass of constracutor");  
  18.     }  
  19.       
  20.     public static void main(String[] args)...{  
  21.         SuperClass t = new SubClass();  
  22.     }  
  23. }  
  24. 输出结果:  
  25. SuperClass of static block  
  26. SubClass of static block  
  27. SuperClass of constracutor  
  28. SubClass of constracutor  

可见当父类,和子类有Static时,先初始化Static,再初始化子类的Static,再初始化父类的其他成员变量->父类构造方法->子类其他成员变量->子类的构造方法。
父类上层还有父类时,总是先执行最顶层父类的Static-->派生类Static-->派生类Static-->.......-->子类Static-->顶层父类的其他成员变量-->父类构造方法--> 派生类的其他成员变量 --> 派生类构造方法--> ...............-->子类其他成员变量-->子类构造方法
讨论到继承,就不得提一下多态:
如果父类构造方法的代码中有子类中被重写得方法,当执行这样的语句
SuperClass super = new SubClass();
初始化时调用父类的构造方法,是执行父类的原方法,还是执行子类中被重写的方法呢?

[java] view plaincopy
 
 
  1. class SuperClass...{  
  2.     public SuperClass()...{  
  3.         System.out.println("SuperClass of constructor");  
  4.         m();  
  5.     }  
  6.     public void m()...{  
  7.         System.out.println("SuperClass.m()");  
  8.     }  
  9. }  
  10. public class SubClassTest extends SuperClass ...{  
  11.     private int i = 10;  
  12.     public SubClassTest()...{  
  13.         System.out.println("SubClass of constructor");  
  14.         super.m();  
  15.         m();  
  16.     }  
  17.     public void m()...{  
  18.         System.out.println("SubClass.m(): i = " + i);  
  19.     }  
  20.     public static void main(String[] args)...{  
  21.         SuperClass t = new SubClassTest();  
  22.     }  
  23. }  
  24. 可能很多人会认为输出为:  
  25. SuperClass of constructor  
  26. SubClass.m(): i = 10  
  27. SubClass of constructor  
  28. SuperClass.m()  
  29. SubClass.m(): i = 10  
  30. 其实不然!  
  31. 正确输出为:  
  32. SuperClass of constructor  
  33. SubClass.m(): i = 0  
  34. SubClass of constructor  
  35. SuperClass.m()  
  36. SubClass.m(): i = 10  
  37. 在生成对象时,父类调用的M()方法,不是父类的 M()方法,而时子类中被重写了的M()方法!!并且还出现一个怪异的现象,子类的privte  int i 也被父类访问到,这不是和我们说private的成员只能在本类使用的原则相违背了吗?其实我们说的这条原则是编译期间所遵守的,在JAVA程序的编译期间,它只检查语法的合法性,在JAVA的JVM中,即运行期间,不管你声明的什么,对于JVM来说都是透明的,而多态是在运行期间执行的,所以能拿到SubClass的private成员,一点都不奇怪,只是此时还没执行 i = 10,所以在父类的构造方法中调用m()时,系统只能将i赋予系统初值0。  
  38. 下面是我设计的一道完整的初始化例子,可测试你对类的初始化问题是否完整掌握:  
  39. 写出程序运行的结果:  
  40. class A...{  
  41.     private int i = 9;  
  42.     protected static int j;  
  43.     static...{  
  44.         System.out.println("-- Load First SuperClass of static block start!-- ");  
  45.         System.out.println("j = " + j);  
  46.         System.out.println("-- Load First SuperClass of static block End  -- ");  
  47.     }  
  48.       
  49.     public A()...{  
  50.         System.out.println("------- Load SuperClass of structor start --------");  
  51.         System.out.println("Frist print j = " + j);  
  52.         j = 10;  
  53.         m();  
  54.         System.out.println("k = " + k);  
  55.         System.out.println("Second print j = " + j);  
  56.         System.out.println("-----------  Load  SuperClass End    ----------- ");  
  57.     }  
  58.       
  59.     private static int k = getInt();  
  60.           
  61.     public static int getInt()...{  
  62.         System.out.println("Load SuperClass.getInt() ");  
  63.         return 11;  
  64.     }   
  65.     static...{  
  66.         System.out.println("--- Load Second SuperClass of static block!-------");  
  67.         System.out.println("j = " + j);  
  68.         System.out.println("k = " + k);  
  69.         System.out.println("-- Load Second SuperClass of static block End -- ");  
  70.     }  
  71.       
  72.     public void m()...{  
  73.         System.out.println("SuperClass.m() , " + "j = " +j);  
  74.           
  75.     }  
  76. }  
  77.   
  78. class B extends A ...{  
  79.     private  int a = 10;  
  80.       
  81.     static...{  
  82.         System.out.println("---- Load SubClass of static block!------");  
  83.         System.out.println("-- Load SubClass of static block End -- ");  
  84.     }  
  85.       
  86.     public B()...{  
  87.         System.out.println("Load SubClass of structor");  
  88.         m();  
  89.         System.out.println("---   Load SubClass End  ---- ");  
  90.     }  
  91.       
  92.     public void m()...{  
  93.         System.out.println("SubClass.m() ," + "a = " + a );  
  94.     }  
  95. }  
  96.   
  97. public class Test1...{  
  98.     public static void main(String[] args)...{  
  99.         A a = new B();  
  100.     }  
  101. }  
  102. 正确的答案为:  
  103. -- Load First SuperClass of static block start!--   
  104. j = 0  
  105. -- Load First SuperClass of static block End  --  
  106. Load SuperClass.getInt()  
  107. --- Load Second SuperClass of static block!-------  
  108. j = 0  
  109. k = 11  
  110. -- Load Second SuperClass of static block End --  
  111. ---- Load SubClass of static block!------  
  112. -- Load SubClass of static block End --  
  113. ------- Load SuperClass of structor start --------  
  114. Frist print j = 0  
  115. SubClass.m() ,a = 0  
  116. k = 11  
  117. Second print j = 10  
  118. -----------  Load  SuperClass End    -----------  
  119. Load SubClass of structor  
  120. SubClass.m() ,a = 10  
  121. ---   Load SubClass End  ----  

下面需要说明的一点也是至关重要的一点:那就是成员变量的初始化和非static初始化块之间的执行顺序是按照他们出现的先后顺序来执行的

[java] view plaincopy
 
 
    1. public class Test04  
    2. {      
    3.     //下面的这两行代码放置的顺序,跟执行结果是有关系的  
    4.     private  String t1 = test();  
    5.       
    6.     {  
    7.        System.out.println("初始化快!");  
    8.     }  
    9.     //上面的这两行代码放置的顺序,跟执行结果是有关系的  
    10.       
    11.     private String test(){  
    12.         System.out.println("实例变量的执行过程");  
    13.         return "test";  
    14.     }  
    15.   
    16.     public Test04()  
    17.     {  
    18.         System.out.println("构造方法!");  
    19.     }  
    20.   
    21.     public static void main(String[] args)  
    22.     {  
    23.         Test04 t2 = new Test04();  
    24.     }  
    25.       
    26. }  

  实例变量的执行过程

      初始化快!

      构造方法!

public class NonStaticBlock {
{
System.out.println("初始化快!");
}
//下面的这两行代码放置的顺序,跟执行结果是有关系的
private String t1 = test();
//上面的这两行代码放置的顺序,跟执行结果是有关系的

private String test(){
System.out.println("实例变量的执行过程");
return "test";
}

public NonStaticBlock()
{
System.out.println("构造方法!");
}

public static void main(String[] args)
{
NonStaticBlock t2 = new NonStaticBlock();
}

}

初始化快!
实例变量的执行过程
构造方法!

public class NonStaticBlock {

//下面的这两行代码放置的顺序,跟执行结果是有关系的
private String t1 = test();
//上面的这两行代码放置的顺序,跟执行结果是有关系的

private String test(){
System.out.println("实例变量的执行过程");
return "test";
}

public NonStaticBlock()
{
System.out.println("构造方法!");
}
{
System.out.println("初始化快!");
}
public static void main(String[] args)
{
NonStaticBlock t2 = new NonStaticBlock();
}

}

实例变量的执行过程
初始化快!
构造方法!

原文地址:https://www.cnblogs.com/kexianting/p/8527553.html