title: Java Generics initialization of child class in base class
body: <p>I'm pretty new to java and not particularly sure how to initialize a generic type / child class from the 'base' class.<br>
  Essentially I have a bunch of classes that extend the abstract class <code>BaseClass</code> that need to be initialized and added to the <code>instance</code> Map if a key is not present.<br>
  The child class is re-used multiple times but is dynamically created based on the <code>key</code> parameter. </p>
  
  <p>I would like to avoid reflection and don't mind changing the template if it's not 'the Java way'.<br>
  What I currently have:</p>
  
  <pre class="lang-java prettyprint-override"><code>public abstract class BaseClass&lt;T&gt; {
      protected Map&lt;String, T&gt; instance = new HashMap&lt;String, T&gt;();
  
      public T Get(String key) {
          if (this.instance.containsKey(key)) {
              return this.instance.get(key);
          } 
          T item = new T(key); // Obviously this line errors but you get the idea
          instance.put(key, item);
          return item;
      }
  }
  
  // Example top class which extends my base class
  public class TopClass extends BaseClass&lt;TopClass&gt; {
      public TopClass(String key) {
          // Do unique initialization stuff
      }
  }
  </code></pre>