The volatile keyword in Java

The Java volatile keyword guarantees that value of the volatile variable will always be read from main memory and not from Thread's local cache. That means, every read of a volatile variable will be read from the computer's main memory, and not from the CPU cache , and that every write to a volatile variable will be written to main memory, and not just to the CPU cache. The Java volatile keyword cannot be used with method or class and it can only be used with a variable.
private volatile boolean open = false;
Java has synchronized methods and synchronized blocks . There is no "synchronized" variable. Using synchronized keyword with variable is illegal and will result in compilation error. Instead of synchronized variable in Java, you can have java volatile variable , which will instruct JVM(Java Virtual Machine) threads to read value of volatile variable from main memory and don’t cache it locally.