Static class - one per JVM or one per app?

J

junk1

I have a class called MyClass that contains a static variable (myVar).

I have two J2EE apps running on WebSphere under the same JVM,
MyFirstApp and MySecondApp. From MyFirstApp i set the value of myVar by
doing MyClass.setMyVar(3).

.....what will be the value of MyClass.getMyVar() when run from
MySecondApp?

ie - will the static var have scope across the whole JVM or just the
web app?

Thanks

David Bevan

http://www.davidbevan.co.uk
 
C

cnpeyton

How do you have MyClass packaged? Is it in both EAR files? It will be
different in the 2 applications if packaged that way. The key isn't
the applications but the classloaders. If you had it in a utility jar
that was in the was/lib/ext directory, then it would be the same for
all applications. You can have a static resource and it can be
different in every classloader.

Chris
 
J

junk1

MyClass is in a jar file and an identical copy of that jar is in the
lib folder of each EAR file (each web app is in a separate ear file)

....would it make any difference if MyClass was in a single jar file
referenced in the classpath of both apps?

How do I know if both apps are using the same classloader or not?
Thanks

David Bevan

http://www.davidbevan.co.uk
 
C

cnpeyton

If you have 2 physical ear files, each with a web module, then you have
different class loaders for the two applications. You will have 2
copies of MyClass, one for each class loader. If you want to get
around this, you would have to move the access of MyClass to a class
loader higher up the chain that would be accessible to both
applications.

A different route would be to have 2 web modeuls in the same EAR. If
you package MyClass into a utility jar and put it in the EAR and then
marked it as available to the 2 different web modules, then i think you
would have only 1 copy of MyClass since there would only be one
application class loader. You do have a class loader for each web
module, but since the class wouldn't be accessible to this class
loader, loading would be delegated to the parent which would be the
application class loader.

There is a utiltiy from IBM called class loader viewer that is a plugin
for WebSphere that will allow you to browse class loaders for
applications and modules. I'll dig up the link and post it here.

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top