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

后端 未结 7 1240
我寻月下人不归
我寻月下人不归 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 05:57

    volatile only has relevance to modifications of the variable itself, not the object it refers to. It makes no sense to have a final volatile field because final fields cannot be modified. Just declare the field final and it should be fine.

提交回复
热议问题