This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Regarding ClassLoader concept

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 22701
129
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
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.
 
Malhar Me
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22701
129
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
Please read our naming policy again.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
All of the following truths are shameless lies. But what about this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic