java final 编译_Java final字段编译时常量表达式

I couldn’t understand the statement changes to the final field may not be observed

它告诉我们,如果最终变量被声明为编译时常量,那么在程序中使用反射API在最终变量中进行的任何更改都将在执行期间对程序不可见.

例如,考虑下面给出的代码:

import java.lang.reflect.*;

class ChangeFinal

{

private final int x = 20;//compile time constant

public static void change(ChangeFinal cf)

{

try

{

Class clazz = ChangeFinal.class;

Field field = clazz.getDeclaredField("x");

field.setAccessible(true);

field.set(cf , 190);//changed x to 190 for object cf

}

catch (Exception ex)

{

ex.printStackTrace();

}

}

public static void main(String[] args)

{

ChangeFinal cf = new ChangeFinal();

System.out.println(cf.x);//prints 20

change(cf);

System.out.println(cf.x);//prints 20

}

}

上面代码的输出是:

20

20

为什么?

答案在于javap -c命令为public static void main提供的输出:

public static void main(java.lang.String[]);

Code:

0: new #3; //class ChangeFinal

3: dup

4: invokespecial #11; //Method "":()V

7: astore_1

8: getstatic #12; //Field java/lang/System.out:Ljava/io/PrintStream;

11: aload_1

12: invokevirtual #13; //Method java/lang/Object.getClass:()Ljava/lang/Cla

ss;

15: pop

16: bipush 20

18: invokevirtual #14; //Method java/io/PrintStream.println:(I)V

21: aload_1

22: invokestatic #15; //Method change:(LChangeFinal;)V

25: getstatic #12; //Field java/lang/System.out:Ljava/io/PrintStream;

28: aload_1

29: invokevirtual #13; //Method java/lang/Object.getClass:()Ljava/lang/Cla

ss;

32: pop

33: bipush 20

35: invokevirtual #14; //Method java/io/PrintStream.println:(I)V

38: return

}

在第16行(在调用changeFinal方法之前),cf.x的值被硬编码为20.在第33行(调用changeFinal方法之后),cf.x的值再次硬编码为20.因此,虽然最终变量x的值的变化在执行期间由反射API成功完成,但由于x是编译时间常数它显示其常量值20.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值