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 began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment