File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ClassLoader and Static variables

 
eshwara moorthy
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64177
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"eshwar", please check your private messages for an important administrative matter.
 
eshwara moorthy
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I've modified my profile as directed.

Thanks
Eshwar
 
Carey Evans
Ranch Hand
Posts: 225
Debian Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
amitabh mehra
Ranch Hand
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
arun nohwar
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
Debian Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • 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.
     
    Kumar Raja
    Ranch Hand
    Posts: 547
    2
    Hibernate Java Spring
    • 0
    • Mark post as helpful
    • send pies
    • 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
     
    Don't get me started about those stupid light bulbs.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic