What is the difference between the new operator and Class.newInstance()?
Solution 1
The new
operator creates a new object of a type that's known statically (at compile-time) and can call any constructor on the object you're trying to create. It's the preferred way of creating an object - it's fast and the JVM does lots of aggressive optimizations on it.
Class.forName().newInstance()
is a dynamic construct that looks up a class with a specific name. It's slower than using new
because the type of object can't be hardcoded into the bytecode, and because the JVM might have to do permissions checking to ensure that you have the authority to create an object. It's also partially unsafe because it always uses a zero-argument constructor, and if the object you're trying to create doesn't have a nullary constructor it throws an exception.
In short, use new
if you know at compile-time what the type of the object is that you want to create. Use Class.forName().newInstance()
if you don't know what type of object you'll be making.
Solution 2
Class.forName("your class name").newInstance()
is useful if you need to instantiate classes dynamically, because you don't have to hard code the class name to create an object.
Imagine a scenario where you load classes dynamically from a remote source. You will know their names but can't import them at compile time. In this case you can't use new
to create new instances. That's (one reason) why Java offers the newInstance()
method.
Solution 3
Class.forName can only call the default constructor (with no parameters) and class name can be provided during runtime e.g. the db-driver name read from a configuration file.
Solution 4
Class.forName("your class name").newInstance()
is used when you want to get an instance form a class work similar to new
, but it is useful when you want to get instance from a class in a jar file or remote server and you can not import it in compile time.
ex: Class.forName("YOUR JDBC DRIVER").newInstance()
, you can not import the JDBC class at compile time.
Solution 5
The major difference is Class.forName('your class name').newInstance()
is dynamic as type need not be hard coded into the code.
i2ijeya
Updated on November 28, 2020Comments
-
i2ijeya over 3 years
What is the difference between
new
operator andClass.forName(...).newInstance()
? Both of them create instances of a class, and I'm not sure what the difference is between them. -
LINEMAN78 over 13 yearsThis is not true you can get constructors that require arguments and use them to instantiate an object through the reflection api. However the newInstance method specifically calls the no-arg constructor and requires that it be visible and accessable. templatetypedef's answer is the most complete/correct and informative.
-
i2ijeya over 13 yearsWhat if I use Class<myClass> inst1 = (Class<myClass>)Class.forName('myClass).getInstance()...... Class<myClass> inst2 = (Class<myClass>)Class.forName('myClass).getInstance().............. This will make multiple instances right.
-
Nipuna over 13 yearsWell I don't find a method getInstance(). Do You?
-
Soumyadip Das almost 12 yearsIts a popular Interview question, & this is a great answer... thanks
-
Manjunath over 9 yearsyou can get constructors that require arguments and use them to instantiate an object through the reflection api.
-
Emad Aghaei over 8 yearsGreat!! It is useful response!!
-
Mark Rotteveel over 7 yearsThe JDBC example is actually not very good, the necessity to call
newInstance()
only existed for some early versions of the MySQL JDBC driver. -
Mark Rotteveel over 7 yearsThe JDBC example is actually not very good, the necessity to call
newInstance()
only existed for some early versions of the MySQL JDBC driver. -
Dávid Horváth over 7 yearsJust a note,
ClassNotFoundException
is checked, so you can't compile this:try { optlib.SomeClass obj = new optlib.SomeClass(); } catch (ClassNotFoundException e) {}
. In this case, you must useClass.forName().newInstance()
. -
Gaurav over 4 yearsflawless answer!