Difference Between Static and Volatile :
Static Variable: If two Threads(suppose t1
and t2
) are accessing the same object and updating a variable which is declared as static then it means t1
and t2
can make their own local copy of the same object(including static variables) in their respective cache, so update made by t1
to the static variable in its local cache wont reflect in the static variable for t2
cache .
Static variables are used in the context of Object where update made by one object would reflect in all the other objects of the same class but not in the context of Thread where update 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 t1
and t2
) are accessing the same object and updating a variable which is declared as volatile then it means t1
and t2
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 update made by one thread to the volatile variable will immediately reflect to the other Thread.