Backend Development 5 min read

Performance Comparison of new Operator and Reflection for Object Creation in Java

The article explains Java's basic concepts of object creation, compares the execution speed of using the new operator versus reflection, analyzes why reflection is slower, and outlines typical scenarios where each method is appropriate, supported by benchmark code and results.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Performance Comparison of new Operator and Reflection for Object Creation in Java

1. Basic Concepts

In Java, objects are usually created with the new operator, but they can also be instantiated via reflection, which allows dynamic creation without compile‑time type knowledge.

//new way to create an object
ReflectDemo reflectDemo = new ReflectDemo();

//Three ways to create an object with reflection
(1) Class
reflectDemoClass = ReflectDemo.class;
(2) Class
aClass = Class.forName("com.whale.springtransaction.transactiondemo.reflectdemo.ReflectDemo");
(3) Class
aClass = reflectDemoClass.getClass();

2. Efficiency Comparison Between new and Reflection

public class ReflectDemo {
    public static void main(String[] args) throws IllegalAccessException, InstantiationException {
        proxyObject();
        newObject();
    }

    // new creates objects
    public static void newObject() {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 100000000; i++) {
            ReflectDemo reflectDemo = new ReflectDemo();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("new time:" + (endTime - startTime));
    }

    // reflection creates objects (three ways shown above, using newInstance())
    public static void proxyObject() throws IllegalAccessException, InstantiationException {
        long startTime = System.currentTimeMillis();
        Class
reflectDemoClass = ReflectDemo.class;
        for (int i = 0; i < 100000000; i++) {
            ReflectDemo reflectDemo = reflectDemoClass.newInstance();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("reflection time:" + (endTime - startTime));
    }
}

The benchmark shows that creating 100 000 000 objects with new is many times faster than using reflection.

3. Reasons for the Large Performance Gap

Java code is compiled to bytecode and executed on the JVM. During runtime the JIT compiler may optimize the bytecode, but reflection relies on dynamic resolution, which prevents many of these optimizations. Specific factors include:

Method#invoke performs argument packaging and unpackaging.

Reflection must check method visibility.

Parameter type checking is required.

Reflective calls are hard to inline.

The JIT cannot optimize reflective invocations.

4. Typical Use Cases for Reflection and new

Reflection scenarios

Spring uses reflection to instantiate beans and place them in the IoC container.

JDBC loads drivers with Class.forName() .

Reverse engineering or decompilation tasks.

Storing a String in a generic ArrayList<Integer> via reflection.

Differences when using new

Objects created with new cannot access private members directly, while reflection can bypass access checks with setAccessible(true) .

new requires the class name at compile time; reflection can instantiate a class without knowing its name beforehand.

JavaperformanceBackend DevelopmentReflectionObject Creation
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.