Java Reference ยท Object-Oriented Concepts
Generic classes and methods in Java
Reference for designing Java generic classes, interfaces and methods: type parameters, diamond inference, generic constructors, static methods and restrictions.
Generics let a class, interface or method work with types that are named later, with the compiler checking the use. List<String> is a list that can only hold strings; you do not cast on the way out and a wrong add fails at compile time rather than with a ClassCastException at run time.
You declare type parameters in angle brackets after the class name (class Box<T>) or before a method's return type (static <T> T first(List<T> list)). By convention single capital letters are used: T (type), E (element), K/V (key/value), R (result).
class Box<T> { private T value; void set(T value) { this.value = value; } T get() { return value; } }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
class Name<T1, T2 extends Bound> { } interface Name<T> { } <T> ReturnType method(T arg) { } <T extends A & B> void method(T arg) { }
| Form | Description |
|---|---|
class Box<T> |
Generic class; T is available in fields, methods and nested non-static types. |
<T> T pick(T a, T b) |
Generic method; the type parameter belongs to the method only. |
new Box<>() |
Diamond (Java 7): the compiler infers the type arguments. |
Main.<String>m() |
Explicit type argument, rarely needed. |
<T extends Number> |
Upper bound; see bounded types. |
<T> Box(T t) |
Generic constructor, even in a non-generic class. |
Usage notes
- Static members cannot use the class's type parameters, because they exist without an instance. A static generic method declares its own
<T>. - No primitives as type arguments. Use
List<Integer>, notList<int>; autoboxing converts in both directions, with some cost. - Not allowed:
new T(),new T[n],T.class,instanceof List<String>, and generic exceptions. Most come from type erasure. - Inference works from arguments and from the target type (
List<String> l = List.of();).varinfers the whole type from the initializer. - Generics are invariant:
List<Integer>is not aList<Number>. Use wildcards for flexible parameters. - Raw types (
Listwithout arguments) turn off checking and exist only for old code. Never use them in new code. - Naming: use meaningful names (
Key,Value) only when single letters would be confusing. - Design tip: make generic classes small and focused. If a class needs five type parameters, a record or interface with fewer is probably a better model.
- Overloading by type argument alone (
void f(List<String>)andvoid f(List<Integer>)) fails because both erase to the same signature.