new Operator vs newInstance() Method in Java
Last Updated :
02 Nov, 2022
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.
In general, the new operator is used to create objects, but if we want to decide the type of object to be created at runtime, there is no way we can use new operator. In this case, we have to use the newInstance() method.
Let us discuss the new operator. In Java, object creation takes place in 3 steps as listed: object instantiation and object initialization, and constructor invocation.
Datatype variable;
As we will use the new keyword, the compiler interprets the variable as an object
Datatype object = new Constructor();
Example:
Java
// Java Program to Illustrate new Operator
// Importing required classes
import java.util.*;
// Main class
class GFG {
// Main drive method
public static void main(String[] args)
{
// List<Integer> al;
// Ny now al is just a variable
// Now creating object using new operator
List<Integer> al = new ArrayList<>();
// Adding elements to above List
al.add(1);
al.add(4);
al.add(3);
// Printing elements of List
System.out.print(al);
}
}
Note: We can use it with constructor too where we wanted to call object not variables.
Now if we come up with to newInstance() method which is present inside java.lang package inside Class class. As we already discussed it is used where we load the class from remote sources.
Consider a scenario where we connect to the database later using our java program for execution. It can be more evidently explained with the JDBC example. Remember there we used the Class.forName() method to load registers dynamically and there we used newInstance() method on top of it to create objects dynamically.
Example:
Java
// Java Program to Demonstrate Working of newInstance()
// Method present inside java.lang.Class
// Class 1
// Class 2
class A {
int a;
}
class B {
int b;
}
// Class 3
// Main class
public class GFG {
// Method 1
// To create an instance of class whose name is
// passed as a string 'c'.
public static void fun(String c)
throws InstantiationException,
IllegalAccessException,
ClassNotFoundException
{
// Creating an object of type 'c'
Object obj = Class.forName(c).newInstance();
// Printing the type of object created
System.out.println("Object created for class:"
+ obj.getClass().getName());
}
// Method 2
// Main driver method
public static void main(String[] args)
throws InstantiationException,
IllegalAccessException,
ClassNotFoundException
{
// Calling above method over "A"
fun("A");
}
}
Output:

Output Explanation: forName() method returns the class 'Class' object on which we are calling newInstance() method which will return the object of that class that we are passing as a command-line argument.
- If the passed class doesn't exist then ClassNotFoundException will occur.
- InstantionException will occur if the passed class doesn't contain the default constructor as newInstance() method internally calls the default constructor of that particular class.
- IllegalAccessException will occur if we don't have access to the definition of the specified class definition.
Related Article: Reflection in Java
Similar Reads
new operator in Java When you are declaring a class in java, you are just creating a new data type. A class provides the blueprint for objects. You can create an object from a class. However obtaining objects of a class is a two-step process : Declaration : First, you must declare a variable of the class type. This vari
5 min read
Constructor newInstance() method in Java with Examples The newInstance() method of a Constructor class is used to create and initialize a new instance of this constructor, with the initialization parameters passed as parameter to this method. Each parameter is unwrapped to match primitive formal parameters, and both primitive and reference parameters ar
3 min read
Static Method vs Instance Method in Java In Java, methods are mainly divided into two parts based on how they are connected to a class, which are the static method and the Instance method. The main difference between static and instance methods is listed below:Static method: A static method is a part of the class and can be called without
4 min read
Array setInt() method in Java The java.lang.reflect.Array.setInt() is an inbuilt method in Java and is used to set a specified int value to a specified index of a given object array. Syntax: Array.setInt(Object []array, int index, int value) Parameter: array: This is an array of type Object which is to be updated. index: This is
3 min read
Singleton Method Design Pattern in Java In object-oriented programming, a Java singleton class is a class that can have only one object (an instance of the class) at a time. After the first time, if we try to instantiate the Java Singleton classes, the new variable also points to the first instance created. So, whatever modifications we d
7 min read