Can volatile variable be defined as static in java?

后端 未结 5 1466
無奈伤痛
無奈伤痛 2021-02-01 04:36

Can I declare something like this??

static volatile boolean first=false;
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-01 05:23

    To expand on Michael's comment.

    static simply means not associated with an instance of the containing class.

    volatile simply means that the value may be changed by other threads without warning.

    So your question boils down to "can a field not associated with an instance of the containing class be changed by another thread without warning?"

    As Michael pointed out, the answer to that question is yes. Instance association is orthogonal to concurrent modification.

提交回复
热议问题