title: Passing Class to a method to instantiate a generic
body: <p>The following code doesn't work, because the marked line does not compile:</p>
  
  <pre><code>MyClass {
      //singleton stuff
      private static MyClass instance;
      private MyClass () {}
      public static MyClass getInstance() {
          if(instance==null) {
              instance = new MyClass ();
          }
          return instance;
      }
  
      // method creating problems
      public NonGenericSuperClassOfGenericClass create(Class&lt;?&gt; c1, Class&lt;?&gt; c2) {
          return new GenericClass&lt;c1,c2&gt;; // DOES NOT COMPILE!!!
      }
  }
  </code></pre>
  
  <p>I do NOT want MyClass to be a generic.
  Why? Because the actual implementation of the method create is similar to the following:</p>
  
  <pre><code>    // method creating problems
      public NonGenericSuperClassOfGenericClass create(Class&lt;?&gt;... classes) {
          if(someCondition)
               return new GenericClass&lt;classes[0],classes[1]&gt;;
          else
               return new OtherGenericClass&lt;classes[0]&gt;;
  }
  </code></pre>
  
  <p>Therefore, I cannot couple MyClass to any particular generics.
  Is there a way to instantiate my GenericClass with passed parameters? How?</p>
  
  <p>Thanks</p>
  
  <hr>
  
  <p>Thank you for your answers. I'll tell you the whole story.</p>
  
  <p>I'm using <strong>Spring</strong> and I plan to use <strong>MongoDB</strong>. </p>
  
  <p>The class GenericClass is something like:</p>
  
  <pre><code> GenericClass&lt;PersistetType1, Long&gt; 
  </code></pre>
  
  <p>or </p>
  
  <pre><code> GenericClass&lt;PersistentType2, Long&gt;
  </code></pre>
  
  <p>where  PersistentType1/2 are classes that I need to finally store in the DB, while, GenericClass is a sort of Proxy to access Mongo APIs. In fact, it looks like:</p>
  
  <pre><code>  public MongoTemplate getTemplate();
    public void save(T toInsert);
    public List&lt;T&gt; select(Query selectionQuery);
    public T selectById(ID id);
    public WriteResult update(Query selectionQuery, Update updatedAttributes);
    public void delete(T toRemove);
    public void delete(Query selectionQuery);
  </code></pre>
  
  <p>Now, what?
  From Controllers (or Entity, if you are picky) I need to instantiate the repository and invoke any methods. This causes the Controllers to be coupled with MongoDB, i.e. they explicitly have to instantiate such GenericClass, which is actually called MongoRepository and is strictly dependent on Mongo (in fact it is a generic with exactly two "degrees of freedom").</p>
  
  <p>So, I decided to create MyClass, that is a further proxy that isolates Controllers. In this way, Controller can get the single instance of MyClass and let it create a new instance of the appropriate repository. In particular, when "somecondition" is true, it means that we want to use MongoRepository (when it is false, maybe, a need to instantiate a Hibernate proxy, i.e. HibernateRepository). However, MongoRepository is generic, therefore it requires some form of instantiation, that I hoped to pass as a parameter. </p>
  
  <p>Unfortunately, generics are resolved at compile time, thus they don't work for me, I guess.</p>
  
  <p>How can I fix that?</p>