Programming
Generic Types
Object-Oriented Programming
Java
Class Instances

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.

Browse interview questions

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:

java
1public class Creator<T> {
2    public T create() {
3        // return new T();  // does not compile
4        return null;
5    }
6}

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().

java
1public class Creator {
2    public static <T> T create(Class<T> type) {
3        try {
4            return type.getDeclaredConstructor().newInstance();
5        } catch (ReflectiveOperationException ex) {
6            throw new IllegalArgumentException("Cannot create " + type.getName(), ex);
7        }
8    }
9
10    public static void main(String[] args) {
11        StringBuilder builder = create(StringBuilder.class);
12        builder.append("hello");
13        System.out.println(builder);
14    }
15}

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.

java
1import java.util.function.Supplier;
2
3public class Creator {
4    public static <T> T create(Supplier<T> supplier) {
5        return supplier.get();
6    }
7
8    public static void main(String[] args) {
9        StringBuilder builder = create(StringBuilder::new);
10        System.out.println(builder);
11    }
12}

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:

java
StringBuilder builder = create(() -> new StringBuilder("seed"));

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.

java
1public interface Factory<T> {
2    T create();
3}
4
5public class Creator {
6    public static <T> T create(Factory<T> factory) {
7        return factory.create();
8    }
9}

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:

java
type.getDeclaredConstructor().newInstance();

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 T directly with new 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
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track 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.

Browse interview questions