Why can an Object member variable not be both final and volatile in Java?

后端 未结 7 1253
我寻月下人不归
我寻月下人不归 2020-12-24 05:37

If in a class I have a ConcurrentHashMap instance that will be modified and read by multiple threads I might define like this:

public class My Class {

    p         


        
7条回答
  •  清歌不尽
    2020-12-24 06:07

    A volatile field gives you guarantees as what happens when you change it. (No an object which it might be a reference to)

    A final field cannot be changed (What the fields reference can be changed)

    It makes no sense to have both.

提交回复
热议问题