title: How can I pass a generic class to a method in Java?
body: <p>Suppose we have a function that creates objects given a particular class:</p>
  
  <pre><code>public static &lt;T&gt; T createObject(Class&lt;T&gt; generic) {
      try {
          return generic.newInstance();
      } catch (Exception ex) {
          return null;
      }
  }
  </code></pre>
  
  <p>We can use the function easily to create instances of non-generic types.</p>
  
  <pre><code>public static void main(String[] args) {
      Foo x = createObject(Foo.class);
  }
  </code></pre>
  
  <p>Is it possible to do the same thing with a generic type?</p>
  
  <pre><code>public static void main(String[] args) {
      ArrayList&lt;Foo&gt; x = createObject(ArrayList&lt;Foo&gt;.class); // compiler error
  }
  </code></pre>