In this article, we will learn the difference between new operator vs newInstance() method. In general, the new operator is used to create the object if you know the type of the object at the beginning itself, but if you don’t know the type of the object at the beginning and if it is passed at the Runtime then we need to go with newInstance() method.
Difference between new operator vs newInstance() method in Java
Before getting into the difference between new operator vs newIntance() method, let’s get some basic understanding of them.
new operator in Java
First of all, new is an operator in Java, it creates a new object of a type that is known beforehand and allocates memory dynamically for the object
Below goes the syntax to use the new keyword
ClassName reference = new ClassName
The ClassName is the name of the class for which the object needs to be created and reference is the variable that refers to the created object. ClassName followed by parenthesis determines the constructor which will be called.
Let’s now try to create an object for Test class using new keyword
package com.javainterviewpoint; public class Test { public Test() { System.out.println("Test class no-args Constructor called!!"); } public static void main(String args[]) { Test t = new Test(); t.disp(); } public void disp() { System.out.println("Disp() method of Test class"); } }
Upon the creation of object, the no-args constructor will be called.
Constructor with the new operator
The new keyword can call any constructor no-args or parameterized constructor. Let’s now remove the no-args constructor in the above code and write a parameterized constructor.
package com.javainterviewpoint; public class Test { public Test(int i) { System.out.println("Test class Parameterized Constructor called!!"); } public static void main(String args[]) { Test t = new Test(10); t.disp(); } public void disp() { System.out.println("Disp() method of Test class"); } }
We had no issues in calling the parameterized constructor using the new keyword.
newInstance() method in Java
newInstance() method is present in java.lang.Class is used to create a new instance of the class dynamically.
Imagine a situation where you load classes dynamically from a remote source and you will not be able to import those classes during compile-time. In those cases we will not able to use the regular new keyword to create the object, we have to go only for the newInstance() method.
Every Java developer should have come across this, have you ever connected to the database to perform any operation?
If yes, then you should have come across this approach. We will be using Class.forName() to load the class dynamically and using the newInstance() method on top of it to create the object dynamically.
Let’s now try to create an object for the Test class dynamically using the newInstance() method.
package com.javainterviewpoint; public class Test { public Test() { System.out.println("Test class no-args Constructor called!!"); } public static void main(String args[]) throws InstantiationException, IllegalAccessException, ClassNotFoundException { Test t = (Test)Class.forName(args[0]).newInstance(); t.disp(); } public void disp() { System.out.println("Disp() method of Test class"); } }
We will have to pass the class name with the fully qualified path as a command-line argument.
javac Test.java
java Test com.javainterviewpoint.Test
Constructor with newInstance() method
Unlike the new operator which can call both no-args and parameterized constructors, but the newInstance() method will only be able to call only the no-args constructor when a class has only parameterized constructors we cannot use newInstance() method.
If there is no no-args constructor and if we still used the newInstance() method to create an object, then we will be getting the below exception
Exception in thread "main" java.lang.InstantiationException: com.javainterviewpoint.Test at java.lang.Class.newInstance(Class.java:427) at com.javainterviewpoint.Test.main(Test.java:12) Caused by: java.lang.NoSuchMethodException: com.javainterviewpoint.Test.() at java.lang.Class.getConstructor0(Class.java:3082) at java.lang.Class.newInstance(Class.java:412) ... 1 more
Test.java
package com.javainterviewpoint; public class Test { public Test(int i) { System.out.println("Test class Parameterized Constructor called!!"); } public static void main(String args[]) throws InstantiationException, IllegalAccessException, ClassNotFoundException { Test t = (Test)Class.forName(args[0]).newInstance(); t.disp(); } public void disp() { System.out.println("Disp() method of Test class"); } }
Let’s now put every possible difference between new operator and newInstance() method in a tabular form
new Operator | newInstance() method |
---|---|
Operator in Java | Method present in java.lang.Class |
new operator can be used to create an object if we know the type of object beforehand | newInstance() method can be used to create an object if we doesn’t know the type of object beforehand and we will be getting at runtime |
Can call any constructor such as no-args constructor and parameterized constructor | Can call only the no-args constructor and it is mandatory to have a no-args constructor |
At runtime, if the .class file is not available, then we will be getting NoClassDefFoundError | At runtime, if the .class file is not available, then we will be getting ClassNotFoundException |
Hope I have covered some of the key difference between new operator vs newInstance() method. Happy Learning !!
Leave a Reply