Volatile Vs Static in JAVA

12 Sep

Static Variable: If two Threads(suppose Thread 1 and Thread 2) are accessing the same object and updating a variable which is declared as static then it means Thread 1 and Thread 2 can make their own local copy of the same object(including static variables) in their respective cache, so updating by Thread 1 to the static variable in its local cache wont reflect in the static variable for Thread 2 cache . Static variables are used in the Object Context where updating by one object would reflect in all the other objects of the same class but not in the Thread context where updating of one thread to the static variable will reflect the changes immediately to all the threads (in their local cache).

Volatile variable: If two Threads(suppose Thread 1 and Thread 2) are accessing the same object and updating a variable which is declared as volatile then it means Thread 1 and Thread 2 can make their own local cache of the Object except the variable which is declared as a volatile . So the volatile variable will have only one main copy which will be updated by different threads and updating by one thread to the volatile variable will immediately reflect to the other Thread. So the volatile variable is used in the Thread context.

for further understanding refer the following image.



Posted by on September 12, 2013 in java


Tags: ,

12 responses to “Volatile Vs Static in JAVA

  1. Monir

    September 13, 2013 at 5:56 pm


  2. Allani Abderraouf

    September 16, 2013 at 1:21 pm


  3. Kalpa

    November 27, 2013 at 3:41 pm

    Please help me to understand. When an variable defined as static; it says There is only one
    occurrence of a class variable per JVM per class loader.
    But your explanation talking about two local copies of static variable. Isn’t it contradict?

    • malalanayake

      November 27, 2013 at 3:59 pm

      Each thread can be seen as having it’s own copy of the field in it’s own local memory space/cache unless the field has been marked with volatile. If you need to share some data with in multiple threads please make it as volatile.

  4. syed

    March 10, 2014 at 4:52 pm

    Great Explanation!!

  5. sekhar

    July 4, 2014 at 7:17 am

    can you please clarify me what is the problem with code:

    Q: Even though i am not declaring the blinker as volatile, but thread t1 able to see the updated value(true) set by the main thread….

    package com.learning;

    public class HowToStopRunningThread implements Runnable{

    * @param args
    public static boolean blinker;

    public static void main(String[] args) {
    Thread t = new Thread(new HowToStopRunningThread());
    HowToStopRunningThread obj = new HowToStopRunningThread();

    public void stop(){
    System.out.println(“Setting the Blinker value”);
    blinker = true;
    }catch(InterruptedException ie){

    public void run() {
    }catch(InterruptedException ie){

    Setting the Blinker value
    and then thread came out of the while loop

    • malalanayake

      July 4, 2014 at 3:42 pm

      The problem is threads can see the changes of static field but it is not referring the same static field in memory. So we cannot guarantee the changes. But if you set variable as volatile that means every thread refers the same memory location and it’s guarantee that the visibility of every changes with in different threads. In your example it may update the value quickly because of the one thread. You better check with more thread then you can see some thread get delay to appear the changes.

  6. amila

    April 3, 2015 at 4:17 am

    In thread context it manage their own local that static variable reflect their values may be some time delay ? I know we cant garanty.but somehow it will reflect their values
    Grate post. Keep going with valuable ideas

  7. Madhava Nadendla

    February 17, 2016 at 8:56 am

    superb explanation…

  8. javaskyway

    October 6, 2016 at 10:31 am

    I don’t know either you have copied this content from
    or stackoverflow members copied your content.

    can you please proof this by taking any simple example in Java


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: