Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java, are static class members shared among programs?

Tags:

I imagine that no, they aren't, because every process has its own memory space, of course.

But how does the whole JVM thing actually work? Is there a separate JVM in a separate process for every Java program that I launch? Do Java programs running in a system share anything at all? Are there differences between OSs and JVM implementations? Can I make programs share variables (i. e. directly through the JVM rather than the usual IPC mechanisms)? Are there more exotic one-process JVMs for special purposes?

Generally, what are recommendable reads about the guts of JVMs? The spec? The source code of some implementation? Websites? Books?

like image 480
Hanno Fietz Avatar asked Mar 26 '09 14:03

Hanno Fietz


People also ask

Are static variables shared between classes?

A static variable acts as a global variable and is shared among all the objects of the class. A non-static variables are specific to instance object in which they are created. Static variables occupies less space and memory allocation happens once.

Are static variables shared?

Class Variables and MethodsA static variable is shared by all instances of a class. Only one variable created for the class.

Are static variables shared by all objects?

static attributes exist independently of any instances of a class and may be accessed even when no instances of the class have been created. You can compare a static variable with a shared variable. A static variable is shared by all the objects of a class.

Can static members be private in Java?

Yes, we can have private methods or private static methods in an interface in Java 9. We can use these methods to remove the code redundancy. Private methods can be useful or accessible only within that interface only.


2 Answers

In Java, are static class members shared among programs?

A class is defined by its full name and the class loader that loaded it. If the same class is within the same JVM process and the two programs loaded the class through the same class loader then the static members are shared. Classloading rules are of extreme importance.

I imagine that no, they aren't, because every process has its own memory space, of course.

If you are using two separate JVMs to launch two apps, you are correct. But take the case of application/servlet containers such as tomcat: they load several apps through the same process (the tomcat host process).

But how does the whole JVM thing actually work? Is there a separate JVM in a separate process for every Java program that I launch? Do Java programs running in a system share anything at all?

Every time you type >java -cp... at the command line, you are creating a new process. Bear in mind that when you run eclipse or call an ant java task with fork=true you are also creating new processes.

Are there differences between OSs and JVM implementations? Can I make programs share variables (i. e. directly through the JVM rather than the usual IPC mechanisms)? Are there more exotic one-process JVMs for special purposes?

Like a poster said, there are projects like Terracota that facilitate this for you. A general approach for this kind of sharing is a distributed cache.

like image 128
Miguel Ping Avatar answered Sep 23 '22 18:09

Miguel Ping


No, static variables aren't between JVMs. Yes, there's a separate JVM process for each Java app you run.

In some implementations I believe they may share some resources (e.g. memory for JITted code of JRE classes) but I'm not sure. I believe there's ongoing work to enable more sharing, but still in a robust way. (You don't really want one JVM crashing to affect others.)

No, you can't make programs share variables transparently.

I believe there are books about the JVM, but I can't recommend any. You'd probably be best off looking for HotSpot white papers for details of that. This one is a pretty good starting point.

like image 22
Jon Skeet Avatar answered Sep 22 '22 18:09

Jon Skeet