jvm 初始化之 <cinit> , <init>
{static T2 t2;C t;static java.lang.String str;public T2();<init>将相应的变量压入<init>方法中 Code: Stack=3, Locals=1, Args_size=1 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: aload_0 5: new #2; //class C 8: dup 9: invokespecial #3; //Method C."<init>":()V 12: putfield #4; //Field t:LC; 15: getstatic #5; //Field java/lang/System.out:Ljava/io/PrintStream; 18: ldc #6; //String -----... 20: invokevirtual #7; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 23: return LineNumberTable: line 11: 0 line 8: 4 line 11: 15public static void main(java.lang.String[]); Code: Stack=2, Locals=4, Args_size=1 0: ldc #8; //String hi 2: astore_1 3: new #9; //class java/lang/StringBuilder 6: dup 7: invokespecial #10; //Method java/lang/StringBuilder."<init>":()V 10: ldc #11; //String ' 12: invokevirtual #12; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 15: aload_1 16: invokevirtual #12; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 19: ldc #11; //String ' 21: invokevirtual #12; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 24: invokevirtual #13; //Method java/lang/StringBuilder.toString:()Ljava/lang/String; 27: astore_2 28: getstatic #14; //Field str:Ljava/lang/String; 31: astore_3 32: return LineNumberTable: line 13: 0 line 14: 3 line 15: 28 line 16: 32static {}; Code: Stack=2, Locals=0, Args_size=0 0: getstatic #5; //Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #15; //String -----.static--- 5: invokevirtual #7; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 8: new #16; //class T2 11: dup 12: invokespecial #17; //Method "<init>":()V//调了T2<init>方法 15: putstatic #18; //Field t2:LT2; 18: ldc #19; //String ff 20: putstatic #14; //Field str:Ljava/lang/String; 23: return LineNumberTable: line 5: 0 line 7: 8 line 9: 18}没注释static T2 t2 = new T2();//调了T2<init>方法结果:-----.static-------cccccc static-...-----...{C t;static java.lang.String str;public T2();<init>将相应的变量压入<init>方法中 Code: Stack=3, Locals=1, Args_size=1 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: aload_0 5: new #2; //class C 8: dup 9: invokespecial #3; //Method C."<init>":()V 12: putfield #4; //Field t:LC; 15: getstatic #5; //Field java/lang/System.out:Ljava/io/PrintStream; 18: ldc #6; //String -----... 20: invokevirtual #7; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 23: return LineNumberTable: line 11: 0 line 8: 4 line 11: 15public static void main(java.lang.String[]); Code: Stack=2, Locals=4, Args_size=1 0: ldc #8; //String hi 2: astore_1 3: new #9; //class java/lang/StringBuilder 6: dup 7: invokespecial #10; //Method java/lang/StringBuilder."<init>":()V 10: ldc #11; //String ' 12: invokevirtual #12; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 15: aload_1 16: invokevirtual #12; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 19: ldc #11; //String ' 21: invokevirtual #12; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 24: invokevirtual #13; //Method java/lang/StringBuilder.toString:()Ljava/lang/String; 27: astore_2 28: getstatic #14; //Field str:Ljava/lang/String; 31: astore_3 32: return LineNumberTable: line 13: 0 line 14: 3 line 15: 28 line 16: 32static {}; Code: Stack=2, Locals=0, Args_size=0 0: getstatic #5; //Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #15; //String -----.static--- 5: invokevirtual #7; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 8: ldc #16; //String ff 10: putstatic #14; //Field str:Ljava/lang/String; 13: return LineNumberTable: line 5: 0 line 9: 8}注释static T2 t2 = new T2(); 没有t2的<init>方法结果:-----.static---
?
总结:当一个类初始化的时候,先调用<cinit>方法 ,如果在<cinit>方法中遇到<init>方法。则先调用完<init>方法。然后在继续执行<cinit>