Java – Why can’t I explicitly pass the type argument to a generic Java method

genericsjavasyntax

I have defined a Java function:

static <T> List<T> createEmptyList() {
    return new ArrayList<T>();
}

One way to call it is like so:

List<Integer> myList = createEmptyList(); // Compiles

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

Object myObject = createEmtpyList<Integer>(); // Doesn't compile. Why?

I get the error Illegal start of expression from the compiler.

Best Solution

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 . < Type > method();

Object list = Collections.<String> emptyList();