• 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 of static inner classes

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
say i have some object x, in which there is (possibly) a static inner class:

Class[] inners=x.getClass.getDeclaredClasses();
for (int i=0; i<inners.length; i++){
if (Modifier.isStatic(inners[i].getModifier())){
...
}
}

I have a reference to x
How do i get a reference to the static inner class ??
NB I mean through reflection!!
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class[] classes = c.getDeclaredClasses(); // returns a reference to all nested classes of 'c'
 
sander hautvast
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but I mentioned this method in my question...

I think I got the whole picture now:
The static inner is not accessible through the instance (as opposed to a static member). it's only accessible through the class (hence static).

So my question becomes irrelevant.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mentioned the method in an erroneous example.
I mentioned the method as the answer.
You seem to be confusing "nested classes" with "inner classes".
There is no such thing as a "static inner class".
In fact, the only difference between a nested class and an inner class is that an inner class must be non-static (Java Language Specification 8.1.2).

Try this:
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tony Morris:
You seem to be confusing "nested classes" with "inner classes".
There is no such thing as a "static inner class".
In fact, the only difference between a nested class and an inner class is that an inner class must be non-static (Java Language Specification 8.1.2).

That is how the JLS defines it, but beware that this definition is not used consistently even in Sun's own literature. In some places, they did talk about "static innner classes". So don't slap those wrists too hard

- Peter
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


That is how the JLS defines it, but beware that this definition is not used consistently even in Sun's own literature. In some places, they did talk about "static innner classes". So don't slap those wrists too hard



Yeah I know - I meant it as a side note rather than the primary cause of the problem. To clarify, the primary cause of the problem is a) your example doesn't compile b) assumptions about how you would get it to compile (as in my provided sample) result in something that does what you want.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic