Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between the new operator and Class.newInstance()?

What is the difference between new operator and Class.forName(...).newInstance()? Both of them create instances of a class, and I'm not sure what the difference is between them.

like image 703
i2ijeya Avatar asked Jan 06 '11 06:01

i2ijeya


People also ask

What does class newInstance () do?

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 is newInstance method in Java?

newInstance() creates a new instance of the class represented by this Class object. The class is instantiated as if by a new expression with an empty argument list. The class is initialized if it has not already been initialized. .

What is the difference between getInstance and new?

getInstance is the static method often used with the Singleton Pattern in Java. The new keyword actually creates a new object. At some point there must be a new (although there are a few other methods to instantiate new objects) to actually create the object that getInstance returns.

Which of the following would you use to instantiate a class in Java a new operator B class forName name?

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.


2 Answers

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.

like image 106
templatetypedef Avatar answered Sep 25 '22 09:09

templatetypedef


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.

like image 27
Andreas Dolk Avatar answered Sep 22 '22 09:09

Andreas Dolk