• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reflection - Instances of the "Class" class

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm currently reading Java Reflection stuff, and was wondering if for a class loaded in a JVM, only one instance of the "Class" class exists. i.e. if I have two instances of a class, and call getClass() on both instances, will I get the same instance of their "Class" class, or two separate instances.

My guess is that the getClass() returns the Singleton instance of the "Class" class.

TIA,
Prasanna
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instances of java.lang.Class are unique to the ClassLoader that loaded them, but if you have more than one ClassLoader, then each one can load the same class file and maintain a separate java.lang.Class object.

Under normal circumstances, this won't come up; it's rather hard to find yourself in a situation where you have copies of the same class loaded multiple times. But it does happen -- usually as a misconfiguration or flat-out-error -- in EJB applications and other situations with distributed/partitioned code.
 
reply
    Bookmark Topic Watch Topic
  • New Topic