Java Generic Classes
In this page:
class ClassName<T> {
private T field;
public void setField(T value) { this.field = value; }
public T getField() { return field; }
}
Defining a Generic Class
To define a generic class, add angle brackets containing a type parameter, such as <T>, right after your class name. You can then use that parameter to define fields, constructors, and method signatures anywhere inside the class body.
Example: Defining a Generic Class
// Define the class Main
public class Main {
// Define the class Box
static class Box<T> {
private T content;
Box(T content) { this.content = content; }
T getContent() { return content; }
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Box object and store it in intBox
Box<Integer> intBox = new Box<>(99);
// Print a line to the console
System.out.println(intBox.getContent());
}
}
Login to try C/C++/Java/PHP code in the editor
Multiple Type Parameters
Generic classes can define multiple type parameters when a single placeholder isn't enough, separating them with commas inside the angle brackets, for example <K, V> as used by Map. Each parameter is substituted independently with a concrete type when the class is actually instantiated.
Example: Multiple Type Parameters
// Define the class Main
public class Main {
// Define the class Pair
static class Pair<K, V> {
K key; V value;
Pair(K key, V value) { this.key = key; this.value = value; }
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Pair object and store it in pair
Pair<String, Integer> pair = new Pair<>("Age", 30);
// Print a line to the console
System.out.println(pair.key + " -> " + pair.value);
}
}
Login to try C/C++/Java/PHP code in the editor
Bounded Type Parameters
You can restrict the types allowed into your generic class using bounded type parameters, which is useful when your class needs to call methods that only exist on certain types. The extends keyword restricts the parameter to a class or interface and its subtypes, giving the compiler enough information to allow those method calls.
Example: Bounded Type Parameters
// Define the class Main
public class Main {
// Define the class NumberBox
static class NumberBox<T extends Number> {
private T num;
NumberBox(T num) { this.num = num; }
double doubled() { return num.doubleValue() * 2; }
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new NumberBox object and store it in box
NumberBox<Integer> box = new NumberBox<>(10);
// Print a line to the console
System.out.println(box.doubled());
}
}
Login to try C/C++/Java/PHP code in the editor
Generic Interface Implementation
Classes can implement generic interfaces in one of two ways: you can either preserve the generic type parameter in your own class definition and stay generic yourself, or specify a concrete data type at implementation time and lock the interface down to just that type.
Example: Generic Interface Implementation
// Define the class Main
public class Main {
// Define the interface Container
interface Container<T> {
T get();
}
// Define the class StringContainer
static class StringContainer implements Container<String> {
public String get() { return "Fixed to String"; }
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new StringContainer object and store it in c
Container<String> c = new StringContainer();
// Print a line to the console
System.out.println(c.get());
}
}
Login to try C/C++/Java/PHP code in the editor
Raw Types and Warnings
Using a generic class without specifying a type parameter creates what's called a 'raw type', a legacy behavior kept around purely for backward compatibility with pre-Generics code. While a raw type still compiles, it triggers compiler warnings and loses all of Generics' type-safety benefits, so it should be avoided in new code.
Example: Raw Types and Warnings
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List rawList = new ArrayList(); // raw type -- compiles but triggers a warning
rawList.add("no type safety");
System.out.println(rawList.get(0));
}
}
Login to try C/C++/Java/PHP code in the editor
- Writing
new T()ornew T[10]inside a generic class, which does not compile becauseTis erased at runtime. - Declaring a
staticfield of typeT, which is not allowed because static members are shared by all instantiations of the class. - Using a raw
Boxwithout a type argument, which compiles with an unchecked warning and can fail later with aClassCastException.
Chapter Quiz — Complete all 4 topics to unlock
0/4 topics done
Complete these topics first: