Static variables and multithreading in java

Is a static member of a class present as only a single instance per process or thread?

static fields have one value per class-loader but I think the meat of your question is in the following:

each thread has its own copy of the static member variable of the class

Each thread may have its own copy of the field in its own local memory space/cache unless the field has been marked with volatile which forces the field to be surrounded with a memory barrier which causes a memory synchronization on each access/update.

Without volatile, any updates and reads to a static field will be made to local thread storage and only updated whenever a thread crosses a memory barrier. Without the memory barriers, there are no guarantees around the order of data operations and when the updates will be shared with other threads.

Here’s a decent page about the Java memory model and a good overview of some of the challenges.

Leave a Comment