<p>I have defined a Java function:</p>

<pre><code>static &lt;T&gt; List&lt;T&gt; createEmptyList() {
    return new ArrayList&lt;T&gt;();
}
</code></pre>

<p>One way to call it is like so:</p>

<pre><code>List&lt;Integer&gt; myList = createEmptyList(); // Compiles
</code></pre>

<p>Why can't I call it by explicitly passing the generic type argument? :</p>

<pre><code>Object myObject = createEmtpyList&lt;Integer&gt;(); // Doesn't compile. Why?
</code></pre>

<p>I get the error <code>Illegal start of expression</code> from the compiler.</p>

[{'answer_id': 24997, 'author': 'Cheekysoft', 'author_id': 1820, 'author_profile': 'https://Stackoverflow.com/users/1820', 'pm_score': 5, 'selected': False, 'text': '<p>You can, if you pass in the type as a method parameter.</p>\n\n<pre><code>static &lt;T&gt; List&lt;T&gt; createEmptyList( Class&lt;T&gt; type ) {\n  return new ArrayList&lt;T&gt;();\n}\n\n@Test\npublic void createStringList() {\n  List&lt;String&gt; stringList = createEmptyList( String.class );\n}\n</code></pre>\n\n<p>Methods cannot be genericised in the same way that a type can, so the only option for a method with a dynamically-typed generic return type -- phew that\'s a mouthful :-) -- is to pass in the type as an argument.</p>\n\n<p>For a truly excellent FAQ on Java generics, <a href="http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html" rel="noreferrer">see Angelika Langer\'s generics FAQ</a>.</p>\n\n<p>.<br>\n.   </p>\n\n<p><strong>Follow-up:</strong></p>\n\n<p>It wouldn\'t make sense in this context to use the array argument as in <code>Collection.toArray( T[] )</code>. The only reason an array is used there is because the same (pre-allocated) array is used to contain the results (if the array is large enough to fit them all in). This saves on allocating a new array at run-time all the time.</p>\n\n<p>However, for the purposes of education, if you did want to use the array typing, the syntax is very similar:</p>\n\n<pre><code>static &lt;T&gt; List&lt;T&gt; createEmptyList( T[] array ) {\n  return new ArrayList&lt;T&gt;();\n}\n\n@Test\npublic void testThing() {\n  List&lt;Integer&gt; integerList = createEmptyList( new Integer[ 1 ] );\n}\n</code></pre>\n'}, {'answer_id': 25010, 'author': 'Henrik Gustafsson', 'author_id': 2010, 'author_profile': 'https://Stackoverflow.com/users/2010', 'pm_score': 0, 'selected': False, 'text': '<p>@pauldoo\nYes, you are quite right. It is one of the weaknesses with the java generics imho.</p>\n\n<p>I response to Cheekysoft I\'d like to propose to also look at how it is done by the Java people themselves, such as T[] <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/AbstractCollection.html" rel="nofollow noreferrer">AbstractCollection#toArray</a>(T[] a). I think Cheekysofts version is superior, but the Java one has the advantage of familiarity.</p>\n\n<p>Edit: Added link.\nRe-edit: Found a bug on SO :)</p>\n\n<hr>\n\n<p>Follow-up on Cheekysoft:\nWell, as it is a list of some type that should be returned the corresponding example should look something like:</p>\n\n<pre><code>static &lt;T&gt; List&lt;T&gt; createEmptyList( List&lt;T&gt; a ) {\n  return new ArrayList&lt;T&gt;();\n}\n</code></pre>\n\n<p>But yes, passing the class object is clearly the better one. My only argument is that of familiarity, and in this exact instance it isn\'t worth much (in fact it is bad).</p>\n'}, {'answer_id': 25877, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 7, 'selected': True, 'text': '<p>When the java compiler cannot infer the parameter type by itself for a static method, you can always pass it using the full qualified method name: Class . &lt; Type > method();</p>\n\n<pre><code>Object list = Collections.&lt;String&gt; emptyList();\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/24991', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/755/']