java类初始化顺序-阿里笔试题

原文链接点击打开链接

阿里笔试题之写出程序输出结果:

[java]  view plain copy
  1. package com.patrick.bishi;  
  2.   
  3. public class TestVar {  
  4.     public static int k = 0;  
  5.     public static TestVar t1 = new TestVar("t1");  
  6.     public static TestVar t2 = new TestVar("t2");  
  7.     public static int i = print("i");  
  8.     public static int n = 99;  
  9.     public int j = print("j");  
  10.     {  
  11.         print("构造");  
  12.     }  
  13.     static {  
  14.         print("静态");  
  15.     }  
  16.   
  17.     public TestVar(String str) {  
  18.         System.out.println((++k) + ":" + str + "    i=" + i + " n=" + n);  
  19.         ++i;  
  20.         ++n;  
  21.     }  
  22.   
  23.     public static int print(String str) {  
  24.         System.out.println((++k) + ":" + str + "    i=" + i + " n=" + n);  
  25.         ++n;  
  26.         return ++i;  
  27.     }  
  28.   
  29.     public static void main(String[] args) {  
  30.         TestVar t = new TestVar("init");  
  31.     }  
  32. }  
结果:

[java]  view plain copy
  1. 1:j i=0 n=0  
  2. 2:构造    i=1 n=1  
  3. 3:t1    i=2 n=2  
  4. 4:j i=3 n=3  
  5. 5:构造    i=4 n=4  
  6. 6:t2    i=5 n=5  
  7. 7:i i=6 n=6  
  8. 8:静态    i=7 n=99  
  9. 9:j i=8 n=100  
  10. 10:构造   i=9 n=101  
  11. 11:init i=10    n=102  

分析:

        初始化一个类(main方法执行之前),主要是对静态成员变量的初始化,过程分为两步:1)按照静态成员变量的定义顺序在类内部声明成员变量;2)按类中成员变量的初始化顺序初始化。

        本例中,先声明成员变量k,t1,t2,i,n,此时,k=i=n=0;t1=t2=null,然后按顺序初始化这几个成员变量,k复制0,t1初始化,执行构造函数,构造函数执行需要先对对象的成员属性先进行初始化,因此执行 j 的初始化,再执行对象的代码块,再是构造函数本身,同理t2初始化,接下来再是 i 和  n 的初始化,然后到main方法中的对象 t 的初始化,对象的初始化同理t1的初始化。

参考http://www.cnblogs.com/lmtoo/archive/2012/04/08/2437918.html,得到此例转换成class的类似代码:

[java]  view plain copy
  1. package com.patrick.bishi;  
  2.   
  3. public class TestVarClass {  
  4.   
  5.     public static int k = 0;  
  6.     public static TestVarClass t1 = null;  
  7.     public static TestVarClass t2 = null;  
  8.     public static int i = 0;  
  9.     public static int n = 0;  
  10.     static {  
  11.         k = 0;  
  12.         t1 = new TestVarClass("t1");  
  13.         t2 = new TestVarClass("t2");  
  14.         i = print("i");  
  15.         n = 99;  
  16.         print("静态");  
  17.     }  
  18.   
  19.     int j = 0;  
  20.   
  21.     public TestVarClass(String str) {  
  22.         j = 0;  
  23.         {  
  24.             j = print("j");  
  25.             print("构造");  
  26.         }  
  27.         System.out.println((++k) + ":" + str + "    i=" + i + " n=" + n);  
  28.         ++i;  
  29.         ++n;  
  30.     }  
  31.   
  32.     public static int print(String str) {  
  33.         System.out.println((++k) + ":" + str + "    i=" + i + " n=" + n);  
  34.         ++n;  
  35.         return ++i;  
  36.     }  
  37.   
  38.     public static void main(String[] args) {  
  39.         TestVarClass t = new TestVarClass("init");  
  40.     }  
  41.   
  42. }  

猜你喜欢

转载自blog.csdn.net/tracy38/article/details/51242247
今日推荐