Module 12: Generics and Type Safety
Type Erasure and Generic Limitations
Learn what Java removes at runtime so generic code makes more sense.
Author
Java Learner Editorial Team
Reviewer
Technical review by Java Learner
Last reviewed
2026-04-17
Java version
Java 25 LTS
Learning goals
- Explain type erasure in plain language
- Recognize the most common generic limitations
- Avoid chasing impossible runtime generic tricks
Java generics mostly live at compile time: The compiler checks types, then much of the generic type detail is erased from runtime representation.
That is why some things are not allowed: You cannot create new T() directly, and arrays of parameterized types are problematic.
This matters when reading framework or utility code: Some patterns exist because of erasure, not because the authors enjoy extra complexity.
Practical goal: Understand the limitation well enough to avoid confusion, not to memorize every corner case.
Runnable examples
A common limitation
class Box<T> {
// T value = new T(); // not allowed
}Expected output
Generic type parameters are not concrete runtime constructors.
Mini exercise
Write down one operation that generics allow at compile time and one thing type erasure prevents at runtime.
Summary
- Generics are primarily a compile-time safety feature.
- Type erasure explains several runtime limitations.
- Understanding the limitation prevents a lot of confusion.
Next step
Close the generics module with a reusable data-structure project.
Sources used