Final variables and synchronization blocks in Java

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

Solution

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

The main reason why local variables end up is that you can use them in anonymous inner classes... It's not about synchronization blocks

Admittedly, final variables are useful for immutable classes - and invariance makes life easier in a multithreaded environment - but this is the only relationship I can think of between the two

Editor: wildezyr's comments make sense without changing the variables you are synchronizing For the reasons he gave, it would be dangerous Is this what "synchronizing variables in blocks" means?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>