final variable java_Final variable and synchronized block in java

问题

What is a final variable in Java? For example: if I write final int temp; in function what is the meaning of the final keyword?

Also, when would I want to use final variable (both as a class variable and as a function variable)?

Why must variables in a synchronized block be declared final?

回答1:

Basically it just means you can't change the value. For instance variables, you have to assign any final variables once (and only once) in the constructor (or with a variable initializer). Synchronization is a pretty orthogonal concept.

The primary reason for making a local variable final is so you can use it in an anonymous inner class... this has nothing to do with being in a synchronized block.

Final variables are useful for immutable classes, admittedly - and immutability makes life easier in a multi-threaded environment - but that's the only relationship between the two that I can think of...

EDIT: Wildwezyr's comment makes sense in terms of not changing the variable on which you are synchronizing. That would be dangerous, for the reasons he's given. Is that what you meant by "variable in synchronized block"?

回答2:

Final variables and synchronized code blocks do have something in common... If you declare non-final variable a and then write synchronized (a) { System.out.println('xxx'); } you will get warning "Synchronization on non-final field" - at least in NetBeans.

Why you should not be synchronizing on non-final field? Because if field value may change, then different threads may be synchronizing on different objects (different values of the field) - so there could be no synchronization at all (every thread may enter synchronized block at the same time).

Look here for example of real-life trouble caused by synchronizing on non-final field: http://forums.sun.com/thread.jspa?threadID=5379204

回答3:

In addition to what Jon Skeet said, the value can't be changed but the contents may be changed.

final Integer one = new Integer(1);

...

one = new Integer(2); // compile error

final List list = new ArrayList();

...

list = new ArrayList(); // compile error

list.add(1); // Changes list, but that's fine!

Also be aware that final and static final are not the same. final is within the scope of the instance, whereas static final is the same for all instances of a class (in other languages this could be called a constant).

Personally I think the advantage of final, even when not absolutely required to get your software working, is in the semantical meaning. It offers you the possibility to say to the compiler and the next person working on that code that this variable is not meant to be changed, and that trying to change it could result in a bug.

来源:https://stackoverflow.com/questions/2101327/final-variable-and-synchronized-block-in-java

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值