JVM Knowledge

A method is generic if it declares one or more type variables (§4.4).

These type variables are known as the type parameters of the method. The form of the type parameter section of a generic method is identical to the type parameter section of a generic class (§8.1.2).

A generic method declaration defines a set of methods, one for each possible invocation of the type parameter section by type arguments. Type arguments may not need to be provided explicitly when a generic method is invoked, as they can often be inferred (§18 (Type Inference)).

The scope and shadowing of a method's type parameter is specified in §6.3 and §6.4.1.

References to a method's type parameter from a nested class or interface are restricted, as specified in §6.5.5.1.

Two methods or constructors M and N have the same type parameters if both of the following are true:

  • M and N have same number of type parameters (possibly zero).
  • Where A_1, ..., A_n are the type parameters of M and B_1, ..., B_n are the type parameters of N, let θ=[B_1:=A_1, ..., B_n:=A_n]. Then, for all i (1 ≤ i ≤ n), the bound of A_i is the same type as θ applied to the bound of B_i.

Where two methods or constructors M and N have the same type parameters, a type mentioned in N can be adapted to the type parameters of M by applying θ, as defined above, to the type.