How do I get a class instance of generic type T?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Java, you cannot write new T() inside generic code. The type parameter T is a compile-time abstraction, and Java generics use type erasure. That means the runtime does not automatically know how to construct an instance of T unless you pass in creation logic explicitly.
Why new T() Does Not Work
A generic type parameter is not a normal class name. By the time the code runs, Java has erased most generic type information. So the compiler cannot turn new T() into a real constructor call.
That is why this is illegal:
The language needs more information: either the class object, a factory, or a constructor reference.
Pass Class<T> When Reflection Is Acceptable
If you truly need runtime construction, one option is to pass a Class<T> and use reflection. The modern form is getDeclaredConstructor().newInstance().
This works only if the target type has an accessible no-argument constructor. That limitation is important. Not every T can be instantiated this way.
Prefer Supplier<T> When You Control the Caller
If the caller knows how to build the object, a Supplier<T> is usually cleaner than reflection.
This is often the best design because:
- it is type-safe
- it avoids reflection
- it supports constructors with arguments by capturing them in a lambda
For example:
Factories Are Better When Creation Has Meaning
Sometimes object creation is domain logic, not just a constructor call. In those cases, a factory interface communicates intent more clearly.
This is useful when construction depends on configuration, validation, pooling, or dependency injection.
Know the Real Question Behind the Requirement
Often "how do I get an instance of generic type T?" really means one of these:
- I need a default constructor call
- I need a caller-supplied creation strategy
- I need dependency injection instead of manual construction
Choosing the right answer depends on which of those problems you actually have. Reflection is flexible, but it is usually not the cleanest default.
Avoid Deprecated Reflection APIs
Older examples often show clazz.newInstance(). Avoid it. It is deprecated because it hides constructor-related exceptions and only works with a public no-argument constructor.
Prefer:
That version is clearer and behaves better with modern exception handling.
Common Pitfalls
- Trying to write
new T()and expecting generics to work like templates with runtime type construction. - Using reflection without checking whether the type has a no-argument constructor.
- Reaching for reflection when a
Supplier<T>or factory would be simpler. - Using deprecated
Class.newInstance()examples from old code. - Assuming every generic type parameter represents a concrete instantiable class.
Summary
- In Java, you cannot instantiate
Tdirectly withnew T(). - Use
Class<T>plus reflection when runtime type construction is genuinely needed. - Prefer
Supplier<T>or a factory when the caller can provide creation logic. - '
getDeclaredConstructor().newInstance()is the modern reflective approach.' - The best solution depends on whether you really need reflection or just a cleaner construction contract.
Related reading
- How do I get a Date without time in Java?
- How do I get my Maven Integration tests to run
- How do I get the file extension of a file in Java?
- How do I get the size of a java.sql.ResultSet?
- How do I handle RabbitMQ Consumer Cancellation Notification when using Spring ChannelAwareMessageListener
- How do I hide .class files from the Open Resource dialog in Eclipse?
- How do i implement Headers Exchange in RabbitMQ using Java?
- How do I implement task prioritization using an ExecutorService in Java 5?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.