It's not a secret anymore!
The moose likes Java in General and the fly likes Reflection and nested classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reflection and nested classes" Watch "Reflection and nested classes" New topic

Reflection and nested classes

Dennis Noren

Joined: Jan 07, 2005
Posts: 15
Is it possible to write code that uses reflection to get information for nested classes without knowing their names? I'm thinking of nested classes that extend the known superclass. In general, my understanding is that a superclass doesn't "know of" its subclasses, but is this an exception to that? I don't see any of the reflection methods that lead me to believe there is a standard way to get at any nested classes.

Java novice, J2SE 1.4.2<br />Some C, some OOA/OOD
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608 getDeclaredClasses() getClasses()

You're right, a superclass never knows about its zero or many subclasses (one of thousands of reasons why a non-final class is an implicit design flaw). You could write an agent using java.lang.instrumentation to maintain a registry of those classes that you're interested in.
[ August 04, 2005: Message edited by: Tony Morris ]

Tony Morris
Java Q&A (FAQ, Trivia)
I agree. Here's the link:
subject: Reflection and nested classes
It's not a secret anymore!