• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ClassLoader and Static variables

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I'm new to Java programming and i've a question regarding how class loader and static keyword works....

When we load the class multiple times in a JVM(is this possible?) what will happen to the static variables state. Will they lose its value? if so then the use of the static keyword is very limited in the sense that preserved static value will be lost when we re-load the class. Can you also please give me some insight into the class loaders.

Thanks
Eshwar
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"eshwar", please check your private messages for an important administrative matter.
 
eshwara moorthy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I've modified my profile as directed.

Thanks
Eshwar
 
Ranch Hand
Posts: 225
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you load a class with the same name in different classloaders, each class will have its own static variables. If this isn't what you want, you should avoid getting the same class from different classloaders. It's hard work to load the same class more than once, unless you're writing servlets.

Please have a look at the Singleton Pattern FAQ for some related information.
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Carey Evans:
... It's hard work to load the same class more than once, unless you're writing servlets...



Carey, can you elaborate on this?
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is not possible to load the class multiple times in a JVM, and for that matter not possible to load the class even twice in JVM.

The above stands true even if there are multiple classloaders in same JVM, because the classloaders exist in JVM in hierarchial (parent-child relationship) only and the classes are loaded using delegation model. (delegation model is defined in wikipedia).

For any practical purpose, the above problem can go bad, if we have 2 version of same jar, abc1.jar and abc2.jar, in the classpath.
I dont know classes of which jar file will be loaded in jvm.

So for any practical purpose, it is best to keep only and exactly one version of any jar file in the classpath.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by arun nohwar:
It is not possible to load the class multiple times in a JVM, and for that matter not possible to load the class even twice in JVM.

The above stands true even if there are multiple classloaders in same JVM, because the classloaders exist in JVM in hierarchial (parent-child relationship) only and the classes are loaded using delegation model. (delegation model is defined in wikipedia).


Oh, but it is possible - as long as you indeed make sure the class loaders that load the class are not in the same hierarchy.

For instance, consider two classes: Test2 located in a folder and Test located in a subfolder called Temp. Temp is not on the classpath (or the system class loader will take over.

Output:

If we add folder Temp to our classpath the result is more what you though it would be:


For any practical purpose, the above problem can go bad, if we have 2 version of same jar, abc1.jar and abc2.jar, in the classpath.
I dont know classes of which jar file will be loaded in jvm.

So for any practical purpose, it is best to keep only and exactly one version of any jar file in the classpath.


And this where we do agree
[ August 05, 2008: Message edited by: Rob Prime ]
 
Carey Evans
Ranch Hand
Posts: 225
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by amitabh mehra:
Carey, can you elaborate on this?


If you're using a web application server like Tomcat or Websphere, you can get the same class loaded more than once in a couple of ways:
  • If you have copies of the same jar in two different WARs, each class will be loaded independently by different classloaders.
  • If you redeploy or restart a WAR, the same class from the same jar will again be loaded independently.
  • In both these cases, the static variables will not be shared.
     
    Ranch Hand
    Posts: 558
    2
    Hibernate Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Rob Prime wrote:

    Originally posted by arun nohwar:
    It is not possible to load the class multiple times in a JVM, and for that matter not possible to load the class even twice in JVM.

    The above stands true even if there are multiple classloaders in same JVM, because the classloaders exist in JVM in hierarchial (parent-child relationship) only and the classes are loaded using delegation model. (delegation model is defined in wikipedia).


    Oh, but it is possible - as long as you indeed make sure the class loaders that load the class are not in the same hierarchy.

    For instance, consider two classes: Test2 located in a folder and Test located in a subfolder called Temp. Temp is not on the classpath (or the system class loader will take over.

    Output:

    If we add folder Temp to our classpath the result is more what you though it would be:


    For any practical purpose, the above problem can go bad, if we have 2 version of same jar, abc1.jar and abc2.jar, in the classpath.
    I dont know classes of which jar file will be loaded in jvm.

    So for any practical purpose, it is best to keep only and exactly one version of any jar file in the classpath.


    And this where we do agree
    [ August 05, 2008: Message edited by: Rob Prime ]



    Hi,
    Can some one explain me, if temp is not in the classpath, how would classloader find the necessary class to load and don't we get class defination not found error. This program is too complicated for my level of knowledge and hence would like to understand and learn on the classloaders. Also, how and why the behaviour changed after adding the temp to class path.

    Thanks
    Kumar
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic