Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 14112
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!