A static
variable of a class
in a process will be shared between every thread contained in that process.
You can verify this by creating a simple class
with a public static
field, and then start up a couple of Threads
and have them increment the variable and see what happens.
If you want to ensure mutual exclusion you can make the variable private, and only allow access to it through methods that are defined using the synchronized
keyword.
class Foo {
private static int aVariable = 0;
public static synchronized void increment() { aVariable++; }
public static synchronized int getVariable() { return aVariable; }
}