aspose file tools
The moose likes Beginning Java and the fly likes (discuss) classes loaded by two different class loaders cannot be cast to one another Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "(discuss) classes loaded by two different class loaders cannot be cast to one another" Watch "(discuss) classes loaded by two different class loaders cannot be cast to one another" New topic
Author

(discuss) classes loaded by two different class loaders cannot be cast to one another

Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 757

I've accepted this statement for so many years but have never asked why it is the case. When two classloaders load the same binary bits of a class (e.g. Foo for discussion sake) from the filesystem, are the two original "bit representations" of Foo decorated with information of the classloader that loaded it ?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24041
    
  13

More or less, yes. Loading a class creates a Class object; that Class knows which loader loaded it. A given ClassLoader will load only one class by a given name. The VM Spec talks about class identity being determined by a (name, ClassLoader) pair; the Class object provides that association.

Each object has a reference to its Class object, and two objects loaded by different loaders can belong to classes with identical names, but distinct Class objects; in this case the two objects are said to belong to different classes.


[Jess in Action][AskingGoodQuestions]
Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 757

I would like to ask another question on classloading.

Imagine I have a service class that looks like this:

In order to call the findAllEmployees method, I would need these classes loaded:

  • ISecurityService
  • SecurityServiceImpl


  • Do I need class Person to be loaded as well ?

    And if Person class has a Address field, do we need to load Address as well ?

    Thanks.
    Ilja Preuss
    author
    Sheriff

    Joined: Jul 11, 2001
    Posts: 14112
    Originally posted by Pho Tek:
    I would like to ask another question on classloading.


    You will have a bigger chance of getting good answers by asking it in a new thread, with a matching subject line.


    The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
     
    I agree. Here's the link: jrebel
     
    subject: (discuss) classes loaded by two different class loaders cannot be cast to one another
     
    Similar Threads
    How to understand this sentence?
    static block
    single type import...??
    Ch10:Development