Why Reflection Slows Down Java Object Creation: New vs Reflective Instantiation

This article compares creating Java objects with the new operator and with reflection, presents benchmark code showing a huge performance gap, explains the underlying reasons such as JIT optimization limits and Method#invoke overhead, and outlines practical scenarios for using each approach.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
Why Reflection Slows Down Java Object Creation: New vs Reflective Instantiation

1. Basic Concepts

In Java, objects are typically created with the new operator, but reflection also allows dynamic instantiation. The article asks when to prefer new versus reflection and how their performance differs.

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

// reflection ways
Class<ReflectDemo> reflectDemoClass = ReflectDemo.class;
Class<?> aClass = Class.forName("com.whale.springtransaction.transactiondemo.reflectdemo.ReflectDemo");
Class<? extends Class> aClass2 = reflectDemoClass.getClass();

2. Efficiency Comparison Between new and Reflection

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 benchmark shows that creating 100,000,000 objects with new is many times faster than using reflection.

Performance comparison chart
Performance comparison chart

The large gap stems from several factors:

Method#invoke performs argument boxing/unboxing and security checks.

Reflection requires visibility checks and parameter validation.

Reflective calls are hard to inline.

JIT cannot optimize reflective invocations effectively.

3. Usage Scenarios for Reflection and new

Typical reflection scenarios

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

JDBC loads database drivers with Class.forName().

Reverse engineering or decompilation tasks.

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

Differences between new and reflection

Objects created with new cannot access private fields directly, while reflection can bypass access checks using setAccessible(). new requires the class name at compile time; reflection can instantiate classes 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.

JavaBackend DevelopmentReflectionObject Creationnew operator
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

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.