title: Is it possible to use a class that is implemented in its parent class (which is an inner class)?
body: <p>Let’s consider a simple class <code>Action</code> which is meant to traverse multiple <em>stages</em>, a pre-defined order of states, until reaching the last stage. The stages should be represented by subclasses of a common class <code>Stage</code> and encapsulate code for the <code>Action</code> class to be executed when a method <code>proceed()</code> is called. This method also returns the next stage.</p>
  
  <p>I hope this is a reasonable design, as I consider it to be cleaner than having an enum with state constants and a <code>proceed()</code> with switch-case.</p>
  
  <p>It can be implemented simply with inner classes:</p>
  
  <pre><code>public class Action {
  
      private int m;
      private Stage stage;
  
      public Action() {
          stage = new FirstStage();
      }
  
      public void proceed() {
          if (stage != null) {
              stage = stage.proceed();
          }
      }
  
      public interface Stage {
  
          /**
           * Executes current stage and returns next stage.
           *
           * @return
           */
          Stage proceed();
      }
  
      public class FirstStage implements Stage {
  
          @Override
          public Stage proceed() {
              m = 100;
              return new SecondStage();
          }
  
      }
  
      public class SecondStage implements Stage {
  
          @Override
          public Stage proceed() {
              //do something
              return null; // last stage
          }
  
      }
  }
  </code></pre>
  
  <p>But I am annoyed by all the <code>Stage</code> subclasses residing loosely in the <code>Action</code> class though they belong together. To solve this and to get a convenient namespacing, it would be useful to implement the stage classes as inner classes of <code>Stage</code>. So we would write <code>Stage.First</code> instead of <code>FirstStage</code> which I consider to be clearer.</p>
  
  <pre><code>public class ActionB {
  
      private int m;
      private Stage stage;
  
      public ActionB() {
          stage = new Stage.First(); // error
      }
  
      public void proceed() {
          if (stage != null) {
              stage = stage.proceed();
          }
      }
  
      public abstract class Stage {
  
          /**
           * Executes current stage and returns next stage.
           *
           * @return
           */
          public abstract Stage proceed();
  
          public class First extends Stage {
  
              @Override
              public Stage proceed() {
                  m = 100;
                  return new Second();
              }
  
          }
  
          public class Second extends Stage {
  
              @Override
              public Stage proceed() {
                  //do something
                  return null; // last stage
              }
  
          }
      }
  }
  </code></pre>
  
  <p>The first thing I had to change was to make <code>Stage</code> an abstract class, because interfaces are static and so the members of <code>Action</code> could not be accessed.</p>
  
  <p>The problem now is to instantiate the first stage in the constructor.
  Compiler error:</p>
  
  <blockquote>
    <p>error: an enclosing instance that contains ActionB.Stage.First is required</p>
  </blockquote>
  
  <p>Sure I understand that an inner class needs an instance of its outer class. But what if the outer class is abstract or even static (an interface)? Shouldn’t this work?</p>
  
  <p>Is there still a way to achieve the nesting? Or is this bad design anyway?</p>