Can I discover a Java class' declared inner classes using reflection?

I think this is what you're after: Class.getClasses().


Class.getDeclaredClasses() is the answer.


package com.test;

public class A {

    public String str;

    public class B {
        private int i;
    }
}
package com.test;

import junit.framework.TestCase;

public class ReflectAB extends TestCase {
    public void testAccessToOuterClass() throws Exception {
           final A a = new A();
           final A.B b = a.new B();
           final Class[] parent = A.class.getClasses();
           assertEquals("com.test.A$B", parent[0].getName());
           assertEquals("i" , parent[0].getDeclaredFields()[0].getName());
           assertEquals("int",parent[0].getDeclaredFields()[0].getType().getName());
           //assertSame(a, a2);
        }

}