Volatile Keyword - ayushmathur94/DirectQuesAns_Prep GitHub Wiki

  • Volatile keyword is used as an indicator to Java compiler and Thread that do not cache the value of this variable and always read it from the main memory.
  • Volatile keyword cannot be used with method or class and it can only be used with a variable.

Accessing a volatile variable synchronizes all the cached copied of the variables in the main memory. Volatile can only be applied to instance variables, which are of type object or private. A volatile object reference can be null.