pauldoo
pauldoo

Reputation: 18645

Why can't I explicitly pass the type argument to a generic Java method?

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.

Upvotes: 32

Views: 30439

Answers (4)

Giuseppe
Giuseppe

Reputation: 1

You can do it as follows, if it is in the same class:

List<String> list = this.<String>createEmptyList();

The compiler of course will tell you that it is "redundant".

Upvotes: 0

Xavier Dury
Xavier Dury

Reputation:

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();

Upvotes: 50

Cheekysoft
Cheekysoft

Reputation: 35590

You can, if you pass in the type as a method parameter.

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

@Test
public void createStringList() {
  List<String> stringList = createEmptyList( String.class );
}

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.

For a truly excellent FAQ on Java generics, see Angelika Langer's generics FAQ.

.
.

Follow-up:

It wouldn't make sense in this context to use the array argument as in Collection.toArray( T[] ). 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.

However, for the purposes of education, if you did want to use the array typing, the syntax is very similar:

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

@Test
public void testThing() {
  List<Integer> integerList = createEmptyList( new Integer[ 1 ] );
}

Upvotes: 25

Henrik Gustafsson
Henrik Gustafsson

Reputation: 54338

@pauldoo Yes, you are quite right. It is one of the weaknesses with the java generics imho.

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[] AbstractCollection#toArray(T[] a). I think Cheekysofts version is superior, but the Java one has the advantage of familiarity.

Edit: Added link. Re-edit: Found a bug on SO :)


Follow-up on Cheekysoft: Well, as it is a list of some type that should be returned the corresponding example should look something like:

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

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).

Upvotes: 0

Related Questions