How do you declare a generic method in Java, and where does the type-parameter section go?
answer
- Type params go before the return type
- `<T>` after modifiers, before return type
- Method owns its own type variable
- Even non-generic classes can have generic methods
- Conventional names: T, E, K, V, R
basics
~20 sYou put the type parameters in angle brackets right before the return type, e.g. static <T> T pick(T a, T b). That <T> introduces a name you can then use for the parameters and return type.
solid answer
~40 sA generic method declares its own type parameters in an angle-bracket section placed immediately before the return type (and after any modifiers): `public static <T> List<T> wrap(T item)`. The `<T>` introduces a type variable scoped to that single method; you then use `T` in the parameter types, return type, and body. This is independent of whether the enclosing class is generic — even a non-generic class can have generic methods. Multiple parameters are comma-separated (`<K, V>`), and they can have bounds (`<T extends Comparable<T>>`). The compiler usually infers the actual types from the arguments at the call site, so callers rarely write the type explicitly. Static methods especially need their own type parameters because they cannot use the class's type parameters.
code
java · 7 linespublic class Util { // not a generic class
public static <T> T firstOrNull(List<T> list) {
return list.isEmpty() ? null : list.get(0);
}
}
String s = Util.firstOrNull(List.of("a", "b")); // T inferred as Stringgo deeper
Can write <T> in the right place and use it in parameters/return; knows the conventional names.
Distinguishes a method's type parameter from a class's; adds bounds and multiple type parameters correctly.
Explains scoping, when static methods require their own type parameters, and how bounds enable calling methods on the type variable.
Frames generic methods as the API tool for expressing relationships between argument and return types without polluting the class signature; guides team conventions.
## What a generic method is A **generic method** is a method that declares one or more **type parameters** (also called *type variables*) of its own. A type parameter is a placeholder name — conventionally a single uppercase letter like `T` (Type), `E` (Element), `K`/`V` (Key/Value), `R` (Result) — that stands for some type the caller will supply or the compiler will figure out. ## The syntax and where the section goes The type-parameter section is a comma-separated list inside angle brackets `< >`. For a **method**, it goes in a specific spot: after the modifiers (`public`, `static`, etc.) and **immediately before the return type**. ``` [modifiers] <TypeParams> ReturnType methodName(params) { ... } ``` Example: ```java public static <T> T firstOrNull(List<T> list) { return list.isEmpty() ? null : list.get(0); } ``` Here `<T>` is the declaration that *introduces* the name `T`. After that point, `T` can be used anywhere in the method's signature (parameter types, return type) and its body. ## Why this differs from a generic class A **generic class** declares its type parameter once, next to the class name: `class Box<T> { ... }`. Every instance method of `Box` can then freely use `T` — it belongs to the instance. A **generic method** instead owns its type parameter for the duration of a single call. The two are independent: - A non-generic class can contain generic methods (the `Collections` utility class is full of them). - A generic class can contain methods that introduce *additional* type parameters beyond the class's own. ## Several type parameters and bounds You can declare more than one: `<K, V>`. You can also constrain a type parameter with a **bound** using `extends`: `<T extends Number>` means *T must be Number or a subtype*. Bounds let the body call methods of the bound type (e.g. `<T extends Comparable<T>>` lets you call `a.compareTo(b)`). ## How the actual type is chosen At the call site the compiler performs **type inference**: it looks at the argument types (and sometimes the target/return context) and deduces what `T` must be. So `firstOrNull(List.of("a", "b"))` infers `T = String`. You can also supply the type explicitly with a *type witness* (covered separately): `Util.<String>firstOrNull(list)`. ## Mental model Think of `<T>` before the return type as a tiny local declaration: 'for the length of this one method call, let `T` mean whatever type fits.' That is why it sits right where it does — it must be introduced before it is first used, and the return type is the first place it appears.
- Can a generic method appear in a non-generic class?Yes. The method declares its own type parameter, independent of the class. `java.util.Collections` (a non-generic utility class) is full of generic methods like `<T> emptyList()`.
- What does a bound like `<T extends Number>` give you inside the body?It lets you treat `T` as a `Number`, so you can call `Number` methods (e.g. `intValue()`) on values of type `T`, and only types that are `Number` or subtypes can be used.
saying these in an interview costs you the question
- Thinking the `<T>` goes after the method name or in the parameter list
- Believing only generic classes can have generic methods
- Confusing the method's `<T>` with a class-level `<T>` — they are separate scopes
- Saying you must always write the type explicitly at the call site