aspose file tools*
The moose likes Java in General and the fly likes ClassLoader and Static variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "ClassLoader and Static variables" Watch "ClassLoader and Static variables" New topic
Author

ClassLoader and Static variables

eshwara moorthy
Greenhorn

Joined: Aug 03, 2008
Posts: 2
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

Joined: Jan 10, 2002
Posts: 61010
    
  65

"eshwar", please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
eshwara moorthy
Greenhorn

Joined: Aug 03, 2008
Posts: 2
Hi

I've modified my profile as directed.

Thanks
Eshwar
Carey Evans
Ranch Hand

Joined: May 27, 2008
Posts: 225

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

Joined: Dec 05, 2006
Posts: 98
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

Joined: Aug 05, 2008
Posts: 16
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

Joined: Oct 27, 2005
Posts: 19670
    
  18

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 ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Carey Evans
Ranch Hand

Joined: May 27, 2008
Posts: 225

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

    Joined: Mar 18, 2010
    Posts: 518
        
        2

    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


    Regards
    KumarRaja

     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: ClassLoader and Static variables