Generic Methods
Generic Methods
In the previous lesson you saw how to make an entire class generic. But sometimes you only need a single method to be generic — the rest of the class can stay concrete. Java lets you declare type parameters directly on a method, independent of whether the enclosing class is generic. This gives you lightweight, reusable utilities without committing to a generic class.
Syntax: where the type parameter goes
The type parameter list lives between the modifiers and the return type:
Breaking that down:
<T>— declares the type parameter for this method only.- The first
Tafter<T>is the return type. - The parameter
T valueuses that same type.
You can have multiple type parameters:
Type inference: the compiler does the work
You almost never have to specify the type argument explicitly when calling a generic method — the compiler infers it from the argument you pass:
identity("hello") the argument is String, so T = String. For identity(42) it is the boxed Integer, so T = Integer. If the arguments conflict, the compiler widens to a common supertype (or Object as the last resort).
A practical example: a generic swap utility
Suppose you need to swap two elements in a list. Without generics you would write one method for each element type. With a generic method you write it once:
java.util.Collections itself is full of them — sort, binarySearch, unmodifiableList are all static generic methods.
Returning a generic type
A generic method can produce a new value, not just operate on its arguments. A classic pattern is a factory or coercion helper:
The compiler infers T from the target type of the assignment. This is called target-type inference and was strengthened in Java 8.
Generic methods in generic classes
A generic method can exist inside a generic class, and its type parameter is completely independent of the class-level one:
Box<T>, T is fixed when you create a Box instance. But U is freshly inferred each time you call map. The two type parameters live at different scopes and do not interfere with each other.
When to use a generic method vs a generic class
- Use a generic method when the type relationship is local to a single operation — it does not need to be remembered across calls.
- Use a generic class when the type is part of the object's identity and must be consistent across multiple method calls (like a
Stack<T>that remembers what type it holds).
<T> and you accidentally declare public <T> void foo(T x) on a method, the method's T silently shadows the class's T. The compiler will warn you in most IDEs. Use a different letter (e.g. U) for method-level parameters inside a generic class.
Summary
Generic methods let you write a single, type-safe algorithm that works across many types, without making the whole class generic. You declare the type parameter just before the return type, and the compiler infers it from the arguments or the assignment target. The pattern is everywhere in the standard library and in well-designed utility classes.