user1630693
user1630693

Reputation: 201

Multiple application on tomcat

If I have two java application running on tomcat. Application A and B. I have a public class with a static variable in Application A then can application B access it. If not then why?

I was asked this in an interview. I said it cannot access. But I did not know the reason why?

Can anyone help with the correct answer.

Upvotes: 4

Views: 672

Answers (5)

Eric Tobias
Eric Tobias

Reputation: 3266

It depends. The question is not clear enough to give a yes/no answer in my opinion.

If the application somehow exposes the variable then, naturally, other applications can just ask to get the value. But this is unlikely what the question was supposed to target. But keep it in mind. In interviews, I found it always good to prove that you can think critically and outside of the box.

I guess they wanted to see if you had grasped the concept of static variables and ClassLoaders. A Java application is dynamically linked which means that at runtime, a ClassLoader (which depends on the application) will perform the linking, loading all libraries or delegating the loading. Static variables are referred to by class. If you try to access a static variable, the class' ClassLoader will try to find and load the class. As each application has its own ClassLoader, each application has its own set of variables.

This is not unlike the principle of separation of address space found in older languages. An application runs in a separate address space even if it is hosted by the same machine. Therefore, two applications running alongside each other cannot simply address variables in the other application's address space as it would violate a constraint that is enforced by several watchdogs. But beware, address space can be shared explicitly.

Upvotes: -2

Pranalee
Pranalee

Reputation: 3409

Because each application has its own classloader, though JVM is same. For more on classloader refer: What is a Java ClassLoader?

Upvotes: 4

dharam
dharam

Reputation: 8106

Let me put it in the simplest manner possible.

Qusetion: How do you access a static variable? Answer: It is definitely using the class name.

We have a class as below:

package com.package1;

public ClassA {
    public static String GLOBAL_VARIABLE= "TEST";
}

Question: How does JVM see the ClassA. Answer: It sees as ClassLoaderForA.com.package1.ClassA

Assuming this was loaded using the class loader of Application1

Now, for the second application's class loader, the ClassA doesn't exist. It will not find it. So how can it get the static field from ClassA

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 122006

Because each WebApp have allocated to its own classloaders (Tomcat system ).

Your exact question,about static :

The key is how ClassLoaders are structured. It's entirely possible for two ClassLoaders within one JVM to each load a class and therefore contains separate, independent copies of the static fields.

Prefer to read: Classloader behaviour on Tomcat with multiple applications

Upvotes: 0

David
David

Reputation: 20073

Each application war has a separate class loader, so no, they can't access each others methods.

However, this could be done using something like RMI or some HTTP web service.

Upvotes: 1

Related Questions