static variable behavior in two different application

Two applications are running in a server. That means two EAR are present in server. Now these two application has same jar within it. The jar has one static variable which is used by both applications. First application_1 initializes that static variable to 50. Then application_2 initializes that variable to 100. So what is the value of static variable for application_1? Is it 50 or as it is the same jar file, so value should be overridden to 100.

If so,then please let me know why? As the application should be loaded in different memory location in the server and jar class will be loaded differently, is it?

Jon Skeet
people
quotationmark

You haven't said which server you're using, but typically an application server has multiple classloaders involved - some shared, and some per-application. The Tomcat documentation has a good explanation of its classloaders, for example.

If there's actually just one jar file, and it's in a location loaded by a shared classloader, then there's only one variable.

However, if the two applications each have their own copy of the jar file (for example if it really is embedded within the EAR file) - or it's in a common location but is loaded by a separate classloader for each application - then there'll be two variables.

Basically, each type in each classloader has its own separate static variables.

Of course, if you've got two completely separate server instances, when you're not just in different classloaders but different processes - at which point they will certainly be separate variables.

people

See more on this question at Stackoverflow