title: Java: Iterate through Nested Classes
body: <p>Currently, I have a number of nested classes inside an outer class. Each nested class has an overriden <code>toString</code> method. My goal is to iterate through these nested classes to access the value returned by the overriden <code>toString</code> without initializing every single nested class. I've tried iterating through the array returned by <code>Class#getDeclaredClasses</code></p>
  <p>The simplified outer class:</p>
  <pre><code>class MyClass {
      static class NestedClassOne {
          @Override
          public String toString() {return &quot;One&quot;;}
      }
      static class NestedClassTwo {
          @Override
          public String toString() {return &quot;Two&quot;;}
      }
  }
  </code></pre>
  <p>The method I've tried, using the <code>Class#getDeclaredClasses</code> array:</p>
  <pre><code>        for (Class&lt;?&gt; NestedClass : MyClass.class.getDeclaredClasses()) {
              System.out.println(NestedClass.toString());
          }
  </code></pre>
  <p>This method, to my dismay, only prints the following:</p>
  <pre><code>class ... MyClass$NestedClassTwo
  class ... MyClass$NestedClassOne
  </code></pre>