12

If I have a

public class SomeClass {
    public static final HashMap hashmap = new HashMap();
}

and then I have five different classes with main(String[] args) methods, that I will run.

when they access SomeClass.hashmap, would they be accessing the same HashMap or will they each have their own hashmap created in the JVM?

ycomp
  • 267
  • 3
  • 8
  • possible duplicate of [Do you use static keyword to declare single instance pattern?](http://programmers.stackexchange.com/questions/256308/do-you-use-static-keyword-to-declare-single-instance-pattern) – gnat Sep 20 '15 at 18:16
  • 2
    Are you running all five processes in the same JVM? Have you tried this? What JVM environment are you running in (having them run within an IDE's environment can have a different result than if you are running each of them on the command line, or having all of them exist within an application server? –  Sep 20 '15 at 18:23
  • 5
    Each of these fields will be unique per-classloader. If you have the class loaded by multiple classloaders in a JVM, you'll get multiple instances of the hashmap. – toniedzwiedz Sep 20 '15 at 18:44
  • Really really *really* isolated. Like *really* isolated. Joking aside, this is a good question. – Neil Sep 21 '15 at 07:13

2 Answers2

15

The answer is "each instance of the class within a different class loader is distinct."

If you have two different JVMs (you invoked java MainOne and java MainTwo), these are obviously different class loaders. However, there are corner cases here with things such as nailgun which keeps the JVM running and... things get complex.

You also have situations such as when the application is launched from within an IDE. Some IDEs will spawn a new JVM for running the application, others could be launching a new class loader within their own JVM (I don't think any do this anymore as it made the IDE less than ideally stable). This can get very tricky too.

When running in within an application server environment, this can either be configurable, or you will need to check the specifications. However, realize that every possible combination is possible. You can have isolation of two modules within the same ear (each .war has its own class loader), you can the entire .ear share a class loader, you can have some funky compatibility mode where everything shares the .jar class loader, but is itself in an isolated class loader for the .war, or you can have the entire server use one class loader. Note that some application servers have changed how they work with different versions (JBoss prior to version 5 was not isolated, while after version 5 was, and 4.0.2 had a hierarchal class loader while 4.0.3 reverted to a unified class loader). It's a mess. And thus the awareness of "static is one per class loader" is key.

It is even possible to have different class loaders within a single application as demonstrated in run a executable jar file within java program using class loaders.

So, going back to the answer "each instance of the class within a different class loader is distinct" - and that can take many different forms and edge conditions.

4

In general, for most of the available implementations of JVM, each of your 5 classes will get their own hashmap instances to call and use, because each new main thread will run in a new JVM. A new JVM is created if you use "java" command to run your program.

If you call these 5 main classes in 5 distinct threads from a tester class, they will be run as threads in the same JVM. In that case, they'll be using the same hashmap.

(https://stackoverflow.com/questions/18394560/when-multiple-java-programs-run-on-the-same-machine)

There are two more interesting scenarios where you can look at this question from - one if what if you are running your 5 main classes as separate programs on a server, and second is what if you are using a multitenancy supporting Java SDK.

In case of the application server, usually the same JVM and a new classloader will be used for each new program, so again, each main class gets its own hashmap. For the multitenant environment, the same hashmap gets shared (http://www.ibm.com/developerworks/library/j-multitenant-java/).

  • Happy to help :) But the more I think, the more scenarios keep popping up, with sometimes conflicting whats-and-hows. Will edit if find something more. – Apoorvaa Singh Sep 20 '15 at 22:03
  • 1
    Different .ear files, different .war deployments, different .war within an .ear, configurations of the application.xml within the .ear file ([module isolation, application isolation, compatibility isolation, server isolation](http://www.theserverside.com/news/1364680/Understanding-J2EE-Application-Server-ClassLoading-Architectures)), [different application servers have different configurations](https://en.wikipedia.org/wiki/EAR_(file_format)#Class_isolation) (even different versions of the same app server)... the answer is "one per class loader - which is specific to your environment." –  Sep 20 '15 at 22:20