Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between calling a class constructor and using Class.forName().newInstance

Tags:

I have been trying to understand the difference between using new to instantiate an object vs using Class.forName("A").newInstance();.

I have run the following code for a simple class A which shows using Class.forname("A").newInstance() is 70-100 times slower than using just new A().

I am curious to know why there is such a difference in time, but couldn't figure out. Please someone help me to understand the reason.

public class Main4test {

    public Main4test() {
    }

    static int turns = 9999999;

    public static void main(String[] args) {
        new Main4test().run();
    }

    public void run() {
        System.out.println("method1: " + method1() + "");
        System.out.println("method2:" + method2() + "");
    }

    public long method2() {
        long t = System.currentTimeMillis();
        for (int i = 0; i < turns; i++) {
            try {
                A a = (A) Class.forName("A").newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return System.currentTimeMillis() - t;
    }

    public long method1() {
        long t = System.currentTimeMillis();
        for (int i = 0; i < turns; i++) {
            try {
                A a = new A();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return System.currentTimeMillis() - t;
    }

}

public class A {
    int a;
    public A() {
    a=0;
    }
}
like image 822
karimvai Avatar asked Sep 22 '14 05:09

karimvai


People also ask

What is the difference between class newInstance and constructor newInstance?

Class. newInstance() throws any exception thrown by the constructor, regardless of whether it is checked or unchecked. Constructor. newInstance() always wraps the thrown exception with an InvocationTargetException .

What does class forName () method do?

forName. Returns the Class object associated with the class or interface with the given string name, using the given class loader. Given the fully qualified name for a class or interface (in the same format returned by getName ) this method attempts to locate, load, and link the class or interface.

What is difference between new operator & newInstance () method?

In Java, new is an operator where newInstance() is a method where both are used for object creation. If we know the type of object to be created then we can use a new operator but if we do not know the type of object to be created in beginning and is passed at runtime, in that case, the newInstance() method is used.

Which of the following would you use to instantiate a class in Java class forName?

Class. forName - It will load the Test class and return the Class class object which contains the metadata of Test class Like name, package, constructor, annotations, methods, fields. newInstance() - Used to instantiate new objects using Java Reflection.


1 Answers

A a = new A();

Calls the new operator and the constructor of A directly, where A is mentioned in the source text and has therefore already been loaded and initialized.

A a = (A) Class.forName("A").newInstance();
  • look to see if A has already been loaded
  • load it if necessary
  • initialize it if necessary
  • locate the no-args constructor via reflection
  • call the new operator and the no-args constructor via reflection
  • typecast the result to A
like image 197
user207421 Avatar answered Oct 17 '22 06:10

user207421