<p>Since generics were introduced, Class is parametrized, so that List.class produces Class&lt;List>. This is clear.</p>

<p>What I am not able to figure out is how to get a instance of Class of type which is parametrized itself, i.e. Class&lt;List&lt;String>>. Like in this snippet:</p>

<pre><code>public class GenTest {
    static &lt;T&gt; T instantiate(Class&lt;T&gt; clazz) throws Exception {
        return clazz.newInstance();
    }
    public static void main(String[] args) throws Exception {
        // Is there a way to avoid waring on the line below
        // without using  @SuppressWarnings("unchecked")?
        // ArrayList.class is Class&lt;ArrayList&gt;, but I would like to
        // pass in Class&lt;ArrayList&lt;String&gt;&gt;
        ArrayList&lt;String&gt; l = GenTest.instantiate(ArrayList.class);
    }
}
</code></pre>

<p>I run into variations of this problem quite often and I still don't know, if I just miss something, or if there is really no better way. Thanks for suggestions.</p>

[{'answer_id': 71753, 'author': 'Avi', 'author_id': 1605, 'author_profile': 'https://Stackoverflow.com/users/1605', 'pm_score': 4, 'selected': True, 'text': '<p>The Class class is a run-time representation of a type. Since parametrized types undergo type erasure at runtime, the class object for Class would be the same as for Class&lt;List&lt;Integer>> and Class&lt;List&lt;String>>.</p>\n\n<p>The reason you cannot instantiate them using the .class notation is that this is a special syntax used for class literals. The <a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.8.2" rel="noreferrer">Java Language Specification</a> specifically forbids this syntax when the type is parametrized, which is why List&lt;String>.class is not allowed.</p>\n'}, {'answer_id': 71792, 'author': 'Tom Hawtin - tackline', 'author_id': 4725, 'author_profile': 'https://Stackoverflow.com/users/4725', 'pm_score': 1, 'selected': False, 'text': '<p>Classes represent classes loaded by a class loader, which are raw types. To represent a parameterized type, use java.lang.reflect.ParameterizedType.</p>\n'}, {'answer_id': 71795, 'author': 'Dan Dyer', 'author_id': 5171, 'author_profile': 'https://Stackoverflow.com/users/5171', 'pm_score': 1, 'selected': False, 'text': "<p>I don't think that you can do what you are trying.  Firstly, your instantiate method doesn't know that its dealing with a parameterised type (you could just as easily pass it java.util.Date.class).  Secondly, because of erasure, doing anything particularly specific with parameterised types at runtime is difficult or impossible.</p>\n\n<p>If you were to approach the problem in a different way, there are other little tricks that you can do, like type inference:</p>\n\n<pre><code>public class GenTest\n{\n    private static &lt;E&gt; List&lt;E&gt; createList()\n    {\n        return new ArrayList&lt;E&gt;();\n    }\n\n    public static void main(String[] args)\n    {\n        List&lt;String&gt; list = createList();\n        List&lt;Integer&gt; list2 = createList();\n    }\n}\n</code></pre>\n"}, {'answer_id': 13365240, 'author': 'thSoft', 'author_id': 90874, 'author_profile': 'https://Stackoverflow.com/users/90874', 'pm_score': 0, 'selected': False, 'text': "<p>The only thing you can do is instantiate <code>List&lt;String&gt;</code> <em>directly</em> and call its <code>getClass()</code>:</p>\n\n<pre><code>instantiate(new List&lt;String&gt;() { ... }.getClass());\n</code></pre>\n\n<p>For types with multiple abstract methods like <code>List</code>, this is quite awkward. But unfortunately, calling subclass constructors (like <code>new ArrayList&lt;String&gt;</code>) or factory methods (<code>Collections.&lt;String&gt;emptyList()</code>) don't work.</p>\n"}]
['https://Stackoverflow.com/questions/71585', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/7135/']