在编译生成class文件时,会自动产生两个方法,一个是类的初始化方法<clinit>, 另一个是实例的初始化方法<init>
<clinit>:在jvm第一次加载class文件时调用,包括静态变量初始化语句和静态块的执行
<init>:在实例创建出来的时候调用,包括调用new操作符;调用Class或java.lang.reflect.Constructor对象的newInstance()方法;调用任何现有对象的clone()方法;通过java.io.ObjectInputStream类的getObject()方法反序列化。
- public class T2{
- static{
- System.out.println("-----.static---");
- }
- static T2 t2 = new T2();
- C t =new C();
- static String str = "ff";
- public T2(){System.out.println("-----...");}
- public static void main(String[] ages){
- String a = "hi";
- String s = "'"+a+"'";
- String ni= T2.str;
- }
- }
- class C{
- static{System.out.println("----cccccc static-...");}
- }
这个是test类。下面是字节码
- {
- 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/Str
- ing;)V
- 23: return
- LineNumberTable:
- line 11: 0
- line 8: 4
- line 11: 15
- public 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/l
- ang/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: 32
- static {};
- 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/Str
- ing;)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/Str
- ing;)V
- 23: return
- LineNumberTable:
- line 11: 0
- line 8: 4
- line 11: 15
- public 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/l
- ang/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: 32
- static {};
- 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/Str
- ing;)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>