102

What is the syntax for explicitly giving the type parameters for a generic Java method?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Dr. Hans-Peter Störr
  • 25,298
  • 30
  • 102
  • 139

3 Answers3

97

The following is not the syntax

<ArgType>genericMethod()

It seems the type arguments must come after a dot as in

SomeClass.<ArgType>genericMethod()
this.<ArgType>genericMethod()
p.<ArgType>genericMethod()
super.<ArgType>genericMethod()
SomeClass.super.<ArgType>genericMethod()
SomeClass.this.<ArgType>genericMethod()
Theodore Norvell
  • 15,366
  • 6
  • 31
  • 45
  • 8
    How would this work for an `import static` method? It's not attached to a class or `this`, and as you state the first syntax listed does not work. – Coderer Apr 20 '18 at 07:25
  • 6
    @Coderer Well the static method must be in some class, so you can use `SomeClass.genericMethod()`. If you didn't import the class, then you use the FQN of the class. I'm sure you know this and were hoping for a more satisfying answer. Personally I don't see why the `genericMethod()` syntax couldn't be added to the language; does it create an ambiguity? – Theodore Norvell Apr 27 '18 at 14:33
  • I actually hadn't tried the FQN of the class, I just switched from `import static pack.MyClass.someMethod; someMethod();` to `import pack.MyClass; MyClass.someMethod()`, but of course it's still more verbose than the "wish this worked" counterexample you give in the answer. – Coderer Apr 30 '18 at 08:20
  • 1
    This is called *type witness*. – John McClane Dec 20 '18 at 20:21
  • 1
    Yes. Sometimes *type arguments* are called *type witnesses*. That's good to know, and I hadn't known it before. The JLS uses the term *type argument*. The Generics trail of the Java tutorial sometimes use *type argument* and sometimes uses *type witness*. – Theodore Norvell Dec 30 '18 at 22:10
  • Thank you, you saved me. But this is so awkward. – lnogueir Feb 02 '22 at 03:24
89

According to the Java specification that would be for example:

Collections.<String>unmodifiableSet()

(Sorry for asking and answering my own question - I was just looking this up for the third time. :-)

Pang
  • 9,564
  • 146
  • 81
  • 122
Dr. Hans-Peter Störr
  • 25,298
  • 30
  • 102
  • 139
3

A good example from java.util.Collection of specifying a generic method which defines its own generic type is Collection.toArray where the method signature looks like:

<T> T[] toArray(T[] a);

This declares a generic type T, which is defined on method call by the parameter T[] a and returns an array of T's. So the same instance could call the toArray method in a generic fashion:

Collection<Integer> collection = new ArrayList<Integer>();
collection.add(1);
collection.add(2);

// Call generic method returning Integer[]
Integer[] ints = collection.toArray(new Integer[]{});

// Call generic method again, this time returning an Number[] (Integer extends Number)
Number[] nums = collection.toArray(new Number[]{});

Also, see the java tutorial on generic type parameters.

krock
  • 28,904
  • 13
  • 79
  • 85
  • 8
    Perhaps you could expand this with the part about explicitly giving a type parameter to a call (compare my answer). Then it would be a good canonical answer; as it is it doesn't even answer the question, since the parameter is deduced implicitly by the compiler in the call. :-) – Dr. Hans-Peter Störr Jan 14 '11 at 12:43