Why Reflection Is Much Slower Than new in Java: Performance Test & Usage Guide

This article compares the performance of creating objects with the new operator versus Java reflection, explains why reflection incurs a large overhead, and outlines scenarios where each approach is appropriate, helping developers choose the right technique for their code.

Architect's Tech Stack
Architect's Tech Stack
Architect's Tech Stack
Why Reflection Is Much Slower Than new in Java: Performance Test & Usage Guide

Basic Concepts

In Java, objects are typically created using the new operator, but they can also be instantiated via reflection.

The article asks when to prefer new over reflection and how their efficiencies differ.

// new way
ReflectDemo reflectDemo = new ReflectDemo();

// reflection ways
Class<ReflectDemo> reflectDemoClass = ReflectDemo.class;
Class<?> aClass = Class.forName("com.example.ReflectDemo");
Class<? extends Class> aClass = reflectDemoClass.getClass();

Efficiency Comparison

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

    // new object creation
    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耗时为:" + (endTime - startTime));
    }

    // reflection object creation
    public static void proxyObject() throws IllegalAccessException, InstantiationException {
        long startTime = System.currentTimeMillis();
        Class<ReflectDemo> reflectDemoClass = ReflectDemo.class;
        for (int i = 0; i < 100000000; i++) {
            ReflectDemo reflectDemo = reflectDemoClass.newInstance();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("反射耗时为:" + (endTime - startTime));
    }
}

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

Reasons for the large difference include:

Method#invoke performs argument boxing/unboxing.

Reflection must check method visibility.

Reflection requires parameter validation.

Reflective calls cannot be inlined.

JIT cannot optimize reflective code.

When to Use Reflection vs. new

Reflection use cases:
1. Spring uses reflection to instantiate beans and place them in the IoC container.
2. Loading JDBC drivers with Class.forName().
3. Reverse engineering / decompilation.
4. Storing a String object in a generic List<int> via reflection.
// Differences between new and reflection
1. new cannot access private members directly; reflection can bypass access checks with setAccessible().
2. new requires the class name at compile time; reflection can create instances without knowing the type beforehand.
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PerformanceReflectionObject Creationnew operator
Architect's Tech Stack
Written by

Architect's Tech Stack

Java backend, microservices, distributed systems, containerized programming, and more.

0 followers
Reader feedback

How this landed with the community

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.