This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Regarding ClassLoader concept Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Regarding ClassLoader concept" Watch "Regarding ClassLoader concept" New topic
Author

Regarding ClassLoader concept

Malhar Me
Greenhorn

Joined: Jun 22, 2009
Posts: 27
Hi,

Please read the below section:

" Once a class is loaded into a JVM, the same class (I repeat, the same class) will not be loaded again. This leads to the question of what is meant by "the same class." Similar to the condition that an object has a specific state, an identity, and that an object is always associated with its code (class), a class loaded into a JVM also has a specific identity, which we'll look at now.

In Java, a class is identified by its fully qualified class name. The fully qualified class name consists of the package name and the class name. But a class is uniquely identified in a JVM using its fully qualified class name along with the instance of the ClassLoader that loaded the class. Thus, if a class named Cl in the package Pg is loaded by an instance kl1 of the class loader KlassLoader, the class instance of C1, i.e. C1.class is keyed in the JVM as (Cl, Pg, kl1). This means that the two class loader instances (Cl, Pg, kl1) and (Cl, Pg, kl2) are not one and the same, and classes loaded by them are also completely different and not type-compatible to each other. How many class loader instances do we have in a JVM? The next section explains this.
"

According to them, only once the class is loaded into the JVM. But in next paragraph they created the 2 instances of same class.
Here I am confuse, how it is possible?

This is the link where I found this:
http://onjava.com/pub/a/onjava/2005/01/26/classloading.html?page=1

Please help me clear the concept of ClassLoader.

Thanks in advance.
Malhar
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Using special ClassLoaders (URLClassLoader being the most often used), you can load classes that are not on the class path. Consider the following:
Although the Class instances are loaded from the very same JAR file, and therefore are actually 100% the same, they are still considered unequal because they are loaded using different ClassLoaders. One reason could be that both use the same version of this JAR, but the ClassLoaders use different versions of a JAR file that is used by this JAR file. As a result, instances of the classes can behave differently.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Malhar Me
Greenhorn

Joined: Jun 22, 2009
Posts: 27
Thanks for your response
but still I am confuse with it, I am not getting the exact answer. Accroding to the O'relly comment their is only one the class is loaded in memory and in example they are showing they create 2 instance of same class.

Please see the starting section of Class Loading of O'relly, the url is already provided.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Please read our naming policy again.
Nawapunth Manusitthipol
Greenhorn

Joined: Aug 14, 2009
Posts: 14
The rule is simple, in a classloader, only one class can use a fully qualified name. This means that you can use more than one class loader to load classes with the same fully-qualified name (as Rob Prime shows).

Now you may want to know how can refer to one and another. The answer lies in classloader. If you ask the class loader kl1, you will get the first class and if you ask kl2, you will get the second one. Explicitly, the method '.forName("pg.cl")' is used to get the class. Implicitly, if a class loaded by kl1 and it uses a class name 'pg.cl', the one that is loaded by kl1 will be used.

Hope this helps.


Middle path.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Regarding ClassLoader concept
 
Similar Threads
The static safety
class.forName()
Class loading.
Class loading
Context Class Loader - very very confusing