Generic Classes and Methods
Fast recall
Quick revision
Core answer
class Box. A generic method declares its own parameters before the return type, as in static T first(List values) . The compiler often infers the method argument. Bounds such as > state which operations are available.Whether you know the syntax and where the type parameter goes for classes vs methods
Forgetting the <T> before the return type on a generic method, or thinking a generic method needs a generic class
Mention type inference and bounded type parameters like <T extends Comparable<? super T>>
Interview-ready guide
Interview answer
There are two different things here.
A generic class parameterizes the entire class. You write the type variable in angle brackets right after the class name — class Box of T — and then you can use T anywhere inside: as a field type, a method parameter, or a return type. When you create an instance you fix the type, like Box of String, and from then on that box only holds Strings.
A generic method parameterizes just one method, and importantly it doesn't require the class to be generic. The key syntax detail is where the type parameter goes: you declare it before the return type. So a method that returns the first element of a list looks like static, then angle bracket T, then T as the return type, then first, taking a List of T.
The nice part is type inference — the compiler figures out what T is from the arguments you pass, so you just call first of myList and you don't have to specify the type yourself. You can also bound the type parameter.
If I write angle bracket T extends Comparable of T, then T is restricted to types that implement Comparable, which lets me call compareTo on them inside the method — that's how you'd write a generic max method.
The mistakes I'd watch for: forgetting the angle bracket T before the return type on a generic method, assuming a generic method needs a generic class when it doesn't, and trying to write new T, which doesn't work because of type erasure.
Generic Classes and Methods
Direct Answer
A generic class declares type parameters after its name, as in class Box<T>. A generic method declares its own parameters before the return type, as in static <T> T first(List<T> values). The compiler often infers the method argument. Bounds such as <T extends Comparable<? super T>> state which operations are available.
Code Example
import java.util.*;
// Generic class
class Box<T> {
private T value;
public void set(T value) { this.value = value; }
public T get() { return value; }
}
class Utils {
// Generic method - <T> before the return type
static <T> T first(List<T> list) { return list.get(0); }
// Bounded generic method - T must be Comparable
static <T extends Comparable<? super T>> T max(List<T> list) {
T best = list.get(0);
for (T t : list) if (t.compareTo(best) > 0) best = t;
return best;
}
}
// Usage - T is inferred from the arguments:
Box<String> b = new Box<>(); b.set("hi");
String f = Utils.first(List.of("a", "b")); // T = String
int m = Utils.max(List.of(3, 1, 2)); // T = Integer -> 3
Common Mistakes & Pitfalls
- Missing
<T>on a method. A generic method needs the type parameter before the return type. - Assuming generic methods need a generic class. A static generic method works in any class.
- Trying
new T(). Type erasure means you can't instantiate a type parameter directly. - Skipping bounds. Without
<T extends Comparable<? super T>>you can't callcompareToonT.
Check your understanding
Answer these without looking back at the article.
- What is type inference in generics?
- Why are raw types a bad practice?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.