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

Even then, there are a number of complications. If a final field is

initialized to a compile-time constant expression (§15.28) in the field

declaration, changes to the final field may not be observed, since uses of that

final field are replaced at compile time with the value of the constant

expression.

Can anyone Please give me better explanation for the above. i couldn't understand the statement changes to the final field may not be observed. May with the help of example.

Thanks in advance

解决方案

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

It tells that , if a final variable is declared as compile time constant then any change made in the final variable using reflection API further in program will not be visible to the program during execution.

For example consider the code given below:

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

}

}

The Output of the above code is:

20

20

WHY?

The answer lies in the output provided by javap -c command for 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

}

At line 16 (before changeFinal method is called)the value of cf.x is hardcoded to 20 . And at line 33 (after changeFinal method is called) the value of cf.x is again hardcoded to 20. Therefore , Although the change in the value of final variable x is done successfully by reflection API during execution, but because of x being a compile time constant it is showing its constant value 20.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值