Search code examples
javagenericsjavadocguavasignature

Understanding method signature


I am trying to understand this method signature:

public final <T> FluentIterable<T> transform(Function<? super E,T> function)

(taken from http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/FluentIterable.html#transform%28com.google.common.base.Function%29)

I understand that transform returns a FluentIterable<T>, but I don't understand what the <T> before FluentIterable<T> means?

Further, in Function<? super E,T>, I understand what ? super E means, but I can't imagine how the compiler can check this - isn't E unknown at compile time, so how shall the compiler know, if ? is a supertype of E?


Solution

  • <T> means that this is a method with the generic type parameter T. This is similiar to <T> after a class name (like List<T>), just for methods.

    E is not unknown at compile time. E is also a generic type parameter defined in the class definition:

    public abstract class FluentIterable<E> extends Object implements Iterable<E>
    

    When you create an instance of this class the compiler knows the type of E.