wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes What is the difference between volatile and static keywords in Java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the difference between volatile and static keywords in Java?" Watch "What is the difference between volatile and static keywords in Java?" New topic
Author

What is the difference between volatile and static keywords in Java?

kumaraswamy dapa
Greenhorn

Joined: Feb 26, 2010
Posts: 11
Hi all can anyone tel me the difference between volatile and static keywords?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14342
    
  22

The volatile and static keywords have totally different purposes. It's a bit strange to ask what the difference between them is, because they are completely unrelated to each other.

When a member variable or method is static, then it is a class member instead of an instance member. See Understanding Instance and Class Members for details about what that means exactly.

The volatile keyword has to do with multi-threading. It's a bit hard to explain, but it gives you certain guarantees with regard to multiple threads, for example that when one thread has changed the value of a volatile variable, its new value is immediately visible to other threads (with non-volatile variables this is not always the case because of certain optimizations that the JVM might do). Section 8.3.1.4 of the Java Language Specification explains what it means exactly. If you want to know more about the subject, the book Java Concurrency in Practice is a very good book about multi-threading in Java.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
kumaraswamy dapa
Greenhorn

Joined: Feb 26, 2010
Posts: 11
Thank you for the reply
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: What is the difference between volatile and static keywords in Java?