10

If I declare a variable static and compiled this class into a executable jar. If I start this class using batch file like this:

java MyClass
java MyClass
java MyClass

Would all 3 process share the same variable?

1
  • 1
    Each process has its own memory space. This is true of any kind of program and is not specific to Java. One of features of threads which makes them different from Processes is that they share memory space by default. BTW: Processes can have Shared Memory, however Java's support for this is minimal and rarely used. Commented Apr 29, 2012 at 13:30

2 Answers 2

20

No. The static variable is specific to the JVM instance. More than that, in fact - it's specific to the class loader which loads the class. So if you created three separate class loaders, each responsible for loading MyClass (not just delegating to some common parent) they'd each have a separate, independent static variable in MyClass.

Sign up to request clarification or add additional context in comments.

2 Comments

you mean which process has its own memory space?
@optimus: Certainly each process has its own memory space, but it's more fine-grained than just the per-process level.
2

Static resources are per class loader and therefore, your 3 processes have obviously three different class loaders and hence, would not share the variables.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.