• Post Reply Bookmark Topic Watch Topic
  • New Topic

Class loader Query ??  RSS feed

 
K Gupta
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to load two instance of same class in same JVM ?

in other words

If a class is loaded in JVM ,is it possible to load same class in JVM after making some changes without shutting down JVM ?
 
Rajah Nagur
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two classes of the same definition can be loaded in to one JVM using different classloaders.
Each classloader has a namespace associated and there will be no conflict.

But in the same classloader it will not be possible to load a already loaded class. JVM ignores and returns the earlier loaded class.

...just my thoughts.
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by K Gupta:
Is it possible to load two instance of same class in same JVM ?

in other words

If a class is loaded in JVM ,is it possible to load same class in JVM after making some changes without shutting down JVM ?


A classloader will load each Class only once, and creates exactly one instance of the Class object (this is why static synchronized methods can synchronize on the Class). If you have multiple class loaders then things get a little fuzzy. Usually you don't have to worry about this, but it does creep up from time to time in web apps and RMI.

Basically, if two classloaders load the same class, they will have two different Class objects. Even , an attempt to cast an object created by one Classloader to an object created by the other classloader will fail! There is a forewarning to this - classloaders are hierarchical. When asked for a class, it will see if its parent can load the class and if so, it will use that. So if two classloaders share a parent, the "problem" sort of goes away. Here is some code that demonstrates all this - edit the first line on main() to point to the directory with the classes in it.

The javadoc for ClassLoader talks about this a bit.




Hope this helps you out...
[ August 23, 2006: Message edited by: Ankur Sharma ]
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another example is the following:

Figure shows an application with a main class called MyMainClass. MyMainClass.class will be loaded by the AppClassLoader. MyMainClass creates instances of two class loaders, CustomClassLoader1 and CustomClassLoader2, which are capable of finding the byte codes of a fourth class called Target from some source (say, from a network path). This means the class definition of the Target class is not in the application class path or extension class path. In such a scenario, if MyMainClass asks the custom class loaders to load the Target class, Target will be loaded and Target.class will be defined independently by both CustomClassLoader1 and CustomClassLoader2. This has serious implications in Java. If some static initialization code is put in the Target class, and if we want this code to be executed one and only once in a JVM, in our current setup the code will be executed twice in the JVM: once each when the class is loaded separately by both CustomClassLoaders. If the Target class is instantiated in both the CustomClassLoaders to have the instances target1 and target2 as shown in Figure 1, then target1 and target2 are not type-compatible. In other words, the JVM cannot execute the code:



Target target3 = (Target) target2;
The above code will throw a ClassCastException. This is because the JVM sees these two as separate, distinct class types, since they are defined by different ClassLoader instances. The above explanation holds true even if MyMainClass doesn't use two separate class loader classes like CustomClassLoader1 and CustomClassLoader2, and instead uses two separate instances of a single CustomClassLoader class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!