Static class - one per JVM or one per app?

Discussion in 'Java' started by junk1@davidbevan.co.uk, Dec 20, 2004.

  1. Guest

    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
     
    , Dec 20, 2004
    #1
    1. Advertising

  2. cnpeyton Guest

    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
     
    cnpeyton, Dec 20, 2004
    #2
    1. Advertising

  3. Guest

    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
     
    , Dec 20, 2004
    #3
  4. cnpeyton Guest

    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
     
    cnpeyton, Dec 20, 2004
    #4
  5. cnpeyton Guest

    cnpeyton, Dec 20, 2004
    #5
  6. Guest

    Thanks, what you say sounds promising since I actually want separate
    values of MyVar for each app.

    David Bevan

    http://www.davidbevan.co.uk
     
    , Dec 20, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S01aX3N0YXRl?=

    Quick one - Is SESSION per browser instance or per IP Address?

    =?Utf-8?B?S01aX3N0YXRl?=, Apr 4, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    5,949
    gerry
    Apr 10, 2006
  2. Kevin Hooke

    Re: Handling both MS JVM and Sun JVM

    Kevin Hooke, Aug 26, 2003, in forum: Java
    Replies:
    2
    Views:
    913
  3. Razvan
    Replies:
    1
    Views:
    443
    tony vee
    Sep 10, 2004
  4. Replies:
    0
    Views:
    372
  5. Michael Schuerig
    Replies:
    6
    Views:
    158
    Robert Dober
    Feb 26, 2007
Loading...

Share This Page