Generic Classes in Java
Learn Java generic classes with Box and Pair examples, raw types, reflection metadata, and the limits of runtime type checks.
See how Java generic classes use type parameters to provide compile-time checks. Examples cover Box and Pair, raw types, reflection metadata, bounded types, and common mistakes.
Generic Classes in Java
Generic classes parameterize types using type parameters (<T>, <K, V>, etc.), enabling you to write a single class that works with multiple data types while maintaining compile-time type safety. Instead of Object casts scattered throughout your code, generics let the compiler enforce correct usage.
Introduction
Before generics, writing a Box class meant a choice: store everything as Object and cast at retrieval, or write separate StringBox, IntegerBox, RobotBox classes that duplicated logic. Generics collapse that tradeoff. Box<T> works with any reference type, and the compiler catches type mistakes at every call site.
The mechanism is called type erasure. The compiler replaces a type parameter with Object (or its leftmost bound) in the erased form and inserts casts where needed. Box<String> and Box<Integer> share the same runtime class, and the JVM does not enforce their type arguments on individual objects. Generic declarations can still leave class-file Signature metadata for reflection to inspect; that metadata does not make an arbitrary List<String> validate its elements at runtime.
Examples use Box and Pair, then cover type bounds, raw types, and self-bounded builders.
When to Use Generic Classes
- Building collection classes —
List<T>,Map<K, V>,Set<T> - Writing utility wrappers that operate on any type
- Creating data holders that cache or buffer elements
- Implementing type-safe builders and fluent APIs
When NOT to Use Generic Classes
- The class behavior is identical across all types and no casting is needed — a plain class suffices
- You need to support primitive types directly (use wrapper classes or specialized implementations)
- Introducing generics adds unnecessary complexity for a one-off utility
- You need to serialize to JSON/XML with frameworks that struggle with generics (check framework support first)
Code Example: A Simple Generic Box
public class Box<T> {
private T content;
public void set(T content) {
this.content = content;
}
public T get() {
return content;
}
}
// Usage
Box<String> stringBox = new Box<>();
stringBox.set("Hello"); // type-safe
String value = stringBox.get(); // no cast needed
// This would be a COMPILE ERROR — type mismatch:
// stringBox.set(42); // ❌ Box<String> cannot hold an Integer
Code Example: A Generic Pair
public class Pair<K, V> {
private K key;
private V value;
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() { return key; }
public V getValue() { return value; }
public static <K, V> Pair<K, V> of(K k, V v) {
return new Pair<>(k, v);
}
}
// Usage
Pair<String, Integer> entry = Pair.of("age", 30);
Mermaid Diagram: Generic Class Hierarchy
classDiagram
class Box~T~ {
-T content
+set(T content)
+get() T
}
class Pair~K, V~ {
-K key
-V value
+getKey() K
+getValue() V
+of() Pair
}
class NumericBox~T extends Number~ {
+T getNumeric()
}
Box~T~ <|-- NumericBox~T extends Number~
Common Mistakes
1. Raw Type Usage
Raw types predate generics and exist as an escape hatch for backward compatibility. When Java 1.5 introduced generics, all existing code kept working because the compiler accepts raw types without requiring the generic parameter. This made generics adoptable without breaking the ecosystem.
// WARNING: raw type - defeats the purpose of generics
Box rawBox = new Box();
rawBox.set("Hello");
rawBox.set(42); // compiles but no type check - danger!
String val = (String) rawBox.get(); // ClassCastException: the current value is an Integer
Fix: Always use parameterized types: Box<String>.
The raw type Box is the erased form of Box<T>. Using it bypasses generic checks at the call site. Depending on how it is used, the compiler may report raw-type or unchecked warnings. Legacy APIs may still expose raw types; keep them at the boundary and use parameterized types in new code.
Class<?> is not raw. It is a parameterized Class whose represented type is unknown. By contrast, bare Class omits the type argument and is raw. The type of String.class is Class<String>, while Class.forName(name) returns Class<?> because the class is selected dynamically.
Reflection can inspect generic signatures declared by classes and members. For example, getGenericSuperclass() can return a ParameterizedType, whose getActualTypeArguments() method exposes the declared arguments. This describes a declaration such as a subclass extending ArrayList<String>; it does not mean the JVM checks the elements of every List<String> object.
The following program shows why assigning a raw list to List<String> is unsafe, then copies external values only after checking each element. Compile with javac -Xlint:unchecked GenericBoundaryDemo.java to see the unchecked warnings. The boundary function rejects null and non-string elements.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
public class GenericBoundaryDemo {
static List<String> copyStrings(Collection<?> input) {
List<String> copy = new ArrayList<>(input.size());
for (Object value : input) {
if (!(value instanceof String)) {
throw new IllegalArgumentException("Expected a non-null String");
}
copy.add((String) value);
}
return copy;
}
public static void main(String[] args) {
List raw = new ArrayList();
raw.add(42);
List<String> unchecked = raw; // unchecked conversion warning
try {
String value = unchecked.get(0); // compiler-inserted cast fails
System.out.println(value);
} catch (ClassCastException ex) {
System.out.println("Raw value failed at read time");
}
System.out.println(copyStrings(Arrays.asList("Ada", "Grace")));
try {
copyStrings(raw); // rejected before an invalid value enters the typed copy
} catch (IllegalArgumentException ex) {
System.out.println("Invalid input rejected");
}
}
}
Run javac GenericBoundaryDemo.java && java GenericBoundaryDemo to see the raw-type failure and the checked boundary result:
Raw value failed at read time
[Ada, Grace]
Invalid input rejected
2. Type Mismatch at Call Site
Generics are invariant — Box<String> is not a Box<Object>, even though String extends Object. This is intentional. If it weren’t, you could do this:
List<Integer> ints = new ArrayList<>();
List<Object> objs = ints; // ❌ compile error
objs.add("string");
Integer i = ints.get(0); // ClassCastException — if this were allowed
Arrays have this exact problem, which is why String[] can cause ArrayStoreException when you store through an Object[] reference. Generics sealed this hole. When you need flexibility, use wildcards like Box<? extends Object> — but they come with their own constraints on what you can read or write.
3. Primitive Types Not Supported
Java generics don’t accept primitives — Box<int> is a compile error. Use Box<Integer>, Box<Double>, Box<Boolean> instead. Why? Erasure replaces T with Object, and primitives aren’t objects in the JVM. No object header, no heap presence, no way to assign int to Object.
Wrappers fix this: Integer wraps an int on the heap. When you call intBox.get(), the compiler inserts an unboxing cast — ((Integer) content).intValue(). JIT optimization usually eliminates the allocation cost in hot paths, so you don’t need to worry much in practice.
If you genuinely need primitive-key maps without the wrapper overhead, Trove’s Int2ObjectMap or Fastutil’s IntObjectMap exist for that. Most code doesn’t need that level of tuning.
// Box<int> box = new Box<>(); // compile error - int is not a reference type
Box<Integer> intBox = new Box<>(); // use wrapper Integer instead
Trade-Off Table
| Aspect | Generic Approach | Raw Type / Object Approach |
|---|---|---|
| Type safety | Compile-time enforced | Runtime ClassCastException risk |
| Code verbosity | Slightly more at declaration | Less at declaration, more at cast sites |
| Performance | No runtime penalty (erasure) | No runtime penalty |
| Refactor safety | Rename type param is safe | Rename field risks casts breaking |
| IDE support | Full autocompletion for T |
Limited — IDE sees Object |
Observability Checklist
- Generic type arguments are consistent across the call chain
- No raw type warnings in static analysis (SpotBugs, CheckStyle)
- Serialization frameworks handle generics correctly
-
equals()/hashCode()account for type parameters - Type parameter contracts documented in Javadoc
Security Notes
- Deserialization attacks: Untrusted data deserialized into
Objector raw types can trigger unsafe casts. Generics provide no runtime protection — use input validation and allowlists. - Type arguments are not runtime validation:
Box<String>andBox<Integer>share the same runtime class. Generic declaration signatures may be available to reflection, but the JVM does not enforce an object’s type arguments. Validate untrusted values at the boundary. - Dynamic class loading:
Class.forName(name)can load a class selected by a string. Validate untrusted names against an allowlist before loading or instantiating a class. - Generic reflection metadata: Methods such as
getGenericSuperclass()can expose declared signature information. This metadata describes declarations; it does not validate generic collection contents at runtime.
Pitfalls
-
Bounded type parameters for operations:
Tcannot be used in+/-operators. Bound withT extends Numberto accessNumbermethods likedoubleValue(). -
Generic array creation is illegal:
new T[10]does not compile. UseArray.newInstance(componentType, size)when you have a runtimeClasstoken; it returnsObject. Cast only when the token is known to match the array type you need. An unchecked cast alone does not establish that a genericT[]is safe. -
instanceofwith generics does not work:if (obj instanceof Box<String>)is a compile error — the type argument is erased at runtime. -
Returning
thisfrom a generic method: If a method returnsT, you cannot simply returnthis—thisis the concrete subclass, notT. Use the self-bounded CRTP pattern with aself()helper.
Self-Bounded Types and the Curiously Recurring Template Pattern
The CRTP pattern uses a self-bounded type parameter: class Builder<T extends Builder<T>>. Borrowed from C++ templates, it lets methods return the concrete subtype instead of the generic base — so PersonBuilder.withName() returns PersonBuilder, not Builder<?>. This gives you fluent chaining without separate interfaces for every builder subclass.
public abstract class Builder<T extends Builder<T>> {
protected T self() {
@SuppressWarnings("unchecked")
T result = (T) this;
return result;
}
public T withName(String name) {
return self(); // override in subclass to add field
}
public T withValue(int value) {
return self();
}
}
public class PersonBuilder extends Builder<PersonBuilder> {
private String name;
private int value;
@Override
public PersonBuilder withName(String name) {
this.name = name;
return this;
}
@Override
public PersonBuilder withValue(int value) {
this.value = value;
return this;
}
public Person build() {
return new Person(name, value);
}
}
The self() method casts this back to T. The @SuppressWarnings(“unchecked”) is unavoidable here — the cast is logically sound (the type parameter guarantees this is some subtype of Builder) but the compiler can’t verify it.
Why not just return this? Because this is a PersonBuilder, not a T. Returning this from a method declared as T doesn’t work — the self() helper bridges that gap.
When to use CRTP in generics:
- Building fluent APIs where each method must return the concrete subtype (builder pattern, step builders)
- Implementing self-typed comparison chains where each
thenCompare()call returns the most specific comparator - Creating dominated builders in frameworks like Hibernate Validator or JAXB RI
Limitations:
- The
@SuppressWarnings(“unchecked”)on the cast is a code smell — static analyzers like SpotBugs may flag it - Deep inheritance hierarchies with CRTP become hard to follow
- The pattern does not compose well with intersection bounds (
<T extends Builder<T> & Cloneable>)
Key Takeaways
Here’s what sticks with you after working with generics long enough:
Box<String>andBox<Integer>share a runtime class; the JVM does not enforce their type arguments on instances- Reflection can read generic declaration signatures, but it cannot validate arbitrary collection elements from a type argument
- You cannot use
instanceof Box<String>or createnew T[]; those type arguments are not reified for runtime checks - Raw types (
Boxinstead ofBox<String>) sidestep all this safety — avoid them in new code - Primitives don’t work: no
Box<int>, useBox<Integer>instead
For a deeper look at how generic classes disappear at compile time, see Type Erasure in Java Generics. For writing flexible methods that operate on generic types, Generic Methods in Java covers static utility patterns and type inference.
Interview Questions
class Box<T> {
T content;
T get() { return content; }
void set(T item) { this.content = item; }
}
// Type-safe instantiation
Box<String> stringBox = new Box<>();
String val = stringBox.get(); // no cast needed
stringBox.set(42); // compile error!
The compiler knows get() returns a String when you call it on a Box<String>. Trying to set(42) on a Box<String> fails at compile time, not runtime.
Object erasure.
// Box<int> box = new Box<>(); // compile error!
Box<Integer> box = new Box<>();
box.set(42); // auto-boxed to Integer
int val = box.get(); // auto-unboxed to int
Generics erase T to Object, and primitives have no heap presence. The compiler inserts unboxing casts at retrieval: ((Integer) content).intValue().
Box rawBox = new Box(); // raw type - compiler warns
rawBox.set("hello");
rawBox.set(42); // allowed! no type check
rawBox.set(new Object());
Integer val = (Integer) rawBox.get(); // ClassCastException if wrong type
You can throw anything into a raw Box. Get the cast wrong at retrieval and you get a ClassCastException at runtime.
Box<String> stringBox = new Box<>();
Box<Integer> intBox = new Box<>();
// At runtime, both are just: class Box
stringBox.getClass() == intBox.getClass(); // true!
There is no separate Box<String> class at runtime, so instanceof Box<String> is illegal. Generic signatures on class and member declarations may still be available to reflection.
class Entry<K, V, R> {
K key;
V value;
R metadata;
}
// Usage
Entry<String, Integer, Boolean> entry = new Entry<>();
entry.key = "age";
entry.value = 30;
entry.metadata = true;
Common conventions: K/V for key/value, T for a single type, E for elements, R for return types.
<T> and <T extends Object>?Object. <T extends Object> makes the intent clearer (reference types only) but <T> is the standard convention. Use <T extends Object> only when a framework or linter requires it.new T[10]?new T[10] doesn't compile. Arrays carry runtime type information that generics don't have.
// new T[10] doesn't work - T erases to Object
// new Object[10] is created instead - wrong type
// Array.newInstance returns Object and uses the token's component type
Object array = Array.newInstance(componentType, size);
// A concrete matching token allows a checked cast
String[] strings = (String[]) Array.newInstance(String.class, 10);
The Class<T> token identifies a reifiable component class, allowing Array.newInstance to create an array of that class. A generic method that returns T[] still needs a contract proving its token matches T; a cast does not prove that by itself. A class token also cannot represent an arbitrary parameterized type such as List<String>.
Pair<K, V> can have Pair(K key, V value) and Pair(Pair<K, V> other) — both valid, both use the class-level K and V.Pair<K, V> is a simple two-element holder for related values.
Pair<String, Integer> entry = Pair.of("age", 30);
String key = entry.getKey(); // "age"
Integer val = entry.getValue(); // 30
Use it when a method needs to return two values. For production code, Map.Entry<K, V> from the JDK has clearer semantics and richer methods.
// Approach 1: Objects.requireNonNull() - fail fast
class NonNullBox<T> {
T content;
void set(T item) {
this.content = Objects.requireNonNull(item);
}
}
// Approach 2: Optional<T> - nullability explicit in type
class OptionalBox<T> {
Optional<T> content = Optional.empty();
void set(T item) { this.content = Optional.of(item); }
}
class NumericBox<T extends Number> {
T content;
double toDouble() {
return content.doubleValue(); // OK - Number has this method
}
}
// Usage
NumericBox<Integer> intBox = new NumericBox<>();
NumericBox<Double> dblBox = new NumericBox<>();
double val = intBox.toDouble(); // calls Integer.doubleValue()
Multiple bounds use &: <T extends Number & Comparable>.
equals(T obj) becomes equals(Object obj). Check the runtime type internally.
class Pair<K, V> {
K key;
V value;
@Override
public boolean equals(Object o) {
if (!(o instanceof Pair)) return false;
Pair<?, ?> other = (Pair<?, ?>) o;
return Objects.equals(key, other.key) && Objects.equals(value, other.value);
}
@Override
public int hashCode() {
return Objects.hash(key, value);
}
}
Use instanceof Pair or getClass() inside — you can’t rely on the generic parameter after erasure. Always override both equals() and hashCode() when storing in collections.
hashCode() is declared on Object, so erasure doesn't affect its signature.
class Box<T> {
T content;
@Override
public int hashCode() {
// content.hashCode() works if T extends Object
// which it always does after erasure
return content != null ? content.hashCode() : 0;
}
}
If your hashCode() calls a method on T, that method must be in the bound or in Object. Since all generic types erase to Object (or a subclass), and hashCode() is on Object, calling t.hashCode() on an unbounded T always works.
final and <T> are independent modifiers — one doesn't affect the other.
public final class ImmutablePair<K, V> {
private final K key;
private final V value;
// ... methods
}
// Can still use different type arguments:
ImmutablePair<String, Integer> p1 = new ImmutablePair<>("a", 1);
ImmutablePair<Object, Object> p2 = new ImmutablePair<>(new Object(), new Object());
final means no subclassing, but instantiation with any type argument works fine. Type arguments do not create separate runtime classes, though declaration signatures can still be exposed to reflection.
| Letter | Meaning |
|---|---|
T |
Type |
K / V |
Key / Value |
E |
Element |
N |
Number |
R |
Return type |
class Map<K, V> { } // typical Map
class List<E> { } // typical List
class Future<T, R> { } // typical Future with return type
T and K/V cover most use cases. Multi-letter names are valid but uncommon in idiomatic Java.
// From constructor argument
Box<String> b1 = new Box<>("hello"); // infers Box<String>
// From left side (diamond syntax)
Box<String> b2 = new Box<>(); // infers Box<String>
Box<> b3 = new Box<>(); // compile error - can't infer
// Explicit when inference is ambiguous
List<String> list = new ArrayList<>(); // infers ArrayList<String>
class Box<T> {
T content;
// This static method has its own <T> - independent of the class's T
public static <T> Box<T> of(T value) {
Box<T> box = new Box<>();
box.set(value);
return box;
}
}
// Usage
Box<String> stringBox = Box.of("hello"); // Box<String>
Box<Integer> intBox = Box.of(42); // Box<Integer>
The method’s <T> is scoped only to that method. The class can be non-generic and the method would still work.
Class<T> is a typed reference to a runtime Class object for a reifiable type. It does not carry arbitrary generic arguments into runtime checks. Class<?> represents a class of unknown type and is parameterized, while bare Class is raw. String.class has type Class<String>, and Class.forName(name) returns Class<?>.
// T.class is illegal - erasure removes T
// String.class has type Class<String>
// Creating instances with Class<T>
Class<String> stringClass = String.class;
String instance = stringClass.getDeclaredConstructor().newInstance();
// Generic array creation
String[] array = (String[]) Array.newInstance(String.class, 10);
// Type-safe casting
Object obj = "hello";
String s = stringClass.cast(obj); // no ClassCastException
Generic declarations can retain signature metadata for reflection. For example, getGenericSuperclass() may return a ParameterizedType, whose getActualTypeArguments() method exposes the declared arguments. This describes a declaration, not runtime validation of an arbitrary object’s contents.
// Pattern 1: Concrete type - class is NOT generic
class StringList implements List<String> {
public String get(int i) { /* ... */ }
public void add(String s) { /* ... */ }
}
// Pattern 2: Deferred to class's own parameter
class AnyList<T> implements List<T> {
public T get(int i) { /* ... */ }
public void add(T item) { /* ... */ }
}
List<String> strings = new StringList(); // concrete type
List<Integer> ints = new AnyList<>(); // deferred to T
Optional<T> represents a value that may be absent, without using null.
Optional<String> opt = Optional.of("hello");
// Safe chaining - empty case handled
String result = opt
.map(String::toUpperCase)
.filter(s -> s.length() > 3)
.orElse("default");
// Getting the value explicitly
opt.isPresent(); // true
opt.get(); // "hello"
opt.orElse("default"); // "hello"
opt.orElseGet(() -> computeDefault()); // lazy default
Further Reading
- Generic Methods — writing flexible methods with type parameters
- Wildcards —
? extends Tand? super Tfor flexible type ranges - Type Erasure — how generics are implemented at compile time
- Type Bounds — constraining type parameters with upper and lower bounds
- Bridge Methods — compiler-generated methods from type erasure
- Oracle: Generic Types — official documentation on generic class declarations
- JLS SE 27 §4.8: Raw Types — the language rules for raw types
- JLS SE 27 §5.1.9: Unchecked Conversion — when unchecked conversions require warnings
- Java SE 27
Class<T>API — class tokens and generic-signature reflection methods - Java SE 27
ParameterizedTypeAPI — declared actual type arguments
Conclusion
Generic declarations let the compiler check type usage, but the JVM does not enforce type arguments on object instances. Reflection can inspect generic signatures on declarations, while a Class<?> value represents an unknown class type; bare Class is raw. Validate raw or external values at the boundary before copying them into a typed collection.
Use a bound only when the class needs methods from that bound. For example, <T extends Number> makes doubleValue() available; unbounded <T> is simpler when that method is not needed.
For continued learning, see Generic Methods in Java which covers generic method declarations, type inference, and bounded type parameters in depth.
Category
Related Posts
Abstract Classes in Java: Interfaces and Composition
Compare Java interfaces, abstract classes, and composition, then see when shared state, default methods, or sealed types change the design choice.
Arithmetic Operators in Java: Division, Overflow, Promotion
Compare Java arithmetic with runnable examples of division, negative remainders, overflow, type promotion, and BigDecimal for decimal money.
Java Array Basics: Length, Bounds, Copies, and Resizing
Learn Java arrays through runtime-sized allocation, bounds checks, aliasing, copying, and the fixed length that distinguishes them from ArrayList.