JAVA字节码查看,a的初始化在构造函数中,b是在static中的,详细参考如下:
public class Main {
public static Main singleton = new Main();
public static int a;
public static int b = 0;
public int aa;
public int bb = 0;
private Main() {
super();
a++;
b++;
aa++;
bb++;
}
D:\>"C:\Program Files\Java\jdk1.6.0_43\bin\javap.exe" -classpath c:\ -c Main
Compiled from "Main.java"
public class Main extends java.lang.Object{
public static Main singleton;
public static int a;
public static int b;
public int aa;
public int bb;
public static Main GetInstence();
Code:
0: getstatic #6; //Field singleton:LMain;
3: areturn
public static void main(java.lang.String[]);
Code:
0: invokestatic #7; //Method GetInstence:()LMain;
3: astore_1
4: getstatic #8; //Field java/lang/System.out:Ljava/io/PrintStream;
7: aload_1
8: pop
9: getstatic #3; //Field a:I
12: invokevirtual #9; //Method java/io/PrintStream.println:(I)V
15: getstatic #8; //Field java/lang/System.out:Ljava/io/PrintStream;
18: aload_1
19: pop
20: getstatic #4; //Field b:I
23: invokevirtual #9; //Method java/io/PrintStream.println:(I)V
26: getstatic #8; //Field java/lang/System.out:Ljava/io/PrintStream;
29: aload_1
30: getfield #5; //Field aa:I
33: invokevirtual #9; //Method java/io/PrintStream.println:(I)V
36: getstatic #8; //Field java/lang/System.out:Ljava/io/PrintStream;
39: aload_1
40: getfield #2; //Field bb:I
43: invokevirtual #9; //Method java/io/PrintStream.println:(I)V
46: return
static {};
Code:
0: new #10; //class Main
3: dup
4: invokespecial #11; //Method "":()V
7: putstatic #6; //Field singleton:LMain;
10: iconst_0
11: putstatic #4; //Field b:I
14: return
}