title: How can I get an instance of a subclass of abstract class from a static method of another class?
body: <p>I have the following class structure:</p>
  
  <ul>
  <li><p>DataStore classes:</p>
  
  <ul>
  <li>Parent class: <code>PStore</code>:  <code>public abstract class PStore ...</code></li>
  <li>2 child classes extending it: <code>CStore1</code> and <code>CStore2</code></li>
  <li>Each of the child classes is a N-glton (sorry, not sure what Java term for that is) - it can have only N instances, numbered 1..NMax. </li>
  <li>Each child class has the following methods for accessing that instance: <code>CStore1.getInstance(Id)</code> and <code>CStore2.getInstance(Id)</code></li>
  </ul></li>
  <li><p>Worker classes:</p>
  
  <ul>
  <li>Parent class: <code>public abstract class PWork...</code></li>
  <li>2 child classes extending it: <code>CWork1</code> and <code>CWork2</code></li>
  </ul></li>
  <li><p>Current situation:</p>
  
  <ul>
  <li><p>Class <code>CWork1</code> implemented an inner class with the following code accessing the Store:</p>
  
  <pre><code>protected class fListener implements ActionListener {
          public void actionPerformed(ActionEvent ae) {
              CStore1.getInstance(Id).someMethod();
              # NOTE: someMethod() is implemented in CStore2 as well
          }
      }
  </code></pre></li>
  <li><p>I need to implement the same <code>fListener</code> class in <code>CWork2</code></p></li>
  <li><p>Being a good little software developer, instead of simply copy/pasting <code>fListener</code> from <code>CWork1</code> to <code>CWork2</code> class and then changing <code>CStore1</code> to <code>CStore2</code> in its code, I decided like an idiot to migrate it to a parent class.</p></li>
  </ul></li>
  </ul>
  
  <hr>
  
  <ul>
  <li><p>What I tried to do:</p>
  
  <ul>
  <li><p>Create an abstract method in <code>PStore</code> called <code>getStoreInstance(int Id)</code>: <code>public abstract PStore getStoreInstance(int Id);
  </code></p></li>
  <li><p>Implement that method in <code>CStore1</code> as <code>public PStore getStoreInstance(int Id) { return CStore1.getInstance(Id); }</code></p>
  
  <p>So far, so good. It compiled all 3 Store classes.</p></li>
  <li><p>Copy the fListener code from <code>PWork</code> to <code>CWork1</code> class</p></li>
  <li><p>Obviously, the compiler complained that it does not know anything about CStore1 class and its method <code>getStoreInstance()</code></p></li>
  <li><p>Now, I decided to simply call the newly created <code>getStoreInstance()</code>:</p>
  
  <p>Old code: <code>CStore1.getInstance(Id).someMethod();</code> </p>
  
  <p>New code: <code>PStore.getStoreInstance(Id).someMethod();</code> </p></li>
  </ul></li>
  <li><p><strong>Compiler complaint</strong>: "Cannot make a static reference to the non-static method getStoreInstance(Id) from the type PStore".</p></li>
  <li><p>The only "fix" proposed by Eclipse was to make getStoreInstance() method static, but THAT didn't work either:</p>
  
  <ul>
  <li><p>When I added a <code>static</code> modifier to the abstract classes's declaration of the method: <code>public static abstract PStore getStoreInstance(int Id);</code> - the compiler complained that "The abstract method getStoreInstance in type PStore can only set a visibility modifier, one of public or protected"</p></li>
  <li><p>When I tried to make the method non-abstract: <code>public static PStore getStoreInstance(int Id);</code> it complained that the method needed a body (quite sensibly)</p></li>
  <li><p>When I tried to add a body, it required me to return PStore object... BUT!!!! My abstract class PStore has no constructore, so there's no way I can return PStore object!!!</p></li>
  </ul></li>
  </ul>
  
  <hr>
  
  <p><strong>Am I approaching the problem correctly?</strong> </p>
  
  <p><strong>If not, how should I approach it?</strong></p>
  
  <p><strong>If I am, then how can I resolve the original complaint</strong> about "Cannot make a static reference to the non-static method", in light of the fact that the abstract class isn't letting me make that method static?</p>