Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the meaning of this usage of Java generics?

Tags:

java

generics

I'd like to know what the first <T> represents in the following line of Java code. I've read several tutorials on generics but none of the examples have 2 generics before the method name. Thanks.

public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped);
like image 449
Julian A. Avatar asked Feb 19 '12 08:02

Julian A.


1 Answers

The first <T> is the actual type parameter declaration, i.e. it says that the method is generic and has a type parameter T.

The second <T> is simply part of the method's return type, i.e. the method returns a Provider<T>.

If the first <T> were omitted, the return type Provider<T> would be invalid, since T would not be a recognised identifier/name for a type. T is only recognised as a type because the first <T> introduces it as such.

like image 173
stakx - no longer contributing Avatar answered Sep 30 '22 12:09

stakx - no longer contributing