java 0b_java的一个问题,看下下面的程序为啥a是1,b是0呢?

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值