In Java, the volatile keyword can be applied for only member variables (fields). When a volatile variable is accessed concurrently by threads, its value is updated consistently among threads. In some cases, using volatile can be an alternative to using synchronized code.

Example:

class VolatileExample {
	
	volatile int x;
	
} 

Rules:

  •           The volatile keyword cannot be applied for class, method and local variable.
  •            A final variable cannot be declared volatile.

Related keyword: synchronized. See all keywords in Java.

Other Recommended Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment