• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reflection - Instances of the "Class" class  RSS feed

 
Prasanna Wamanacharya
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • 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
 
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
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!