new Operator vs newInstance() Method in Java - GeeksforGeeks

24 Apr, 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 classesimport java.util.*; // Main classclass 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);    }}
[1, 4, 3]
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 2class A {    int a;}class B {    int b;} // Class 3// Main classpublic 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
This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
solankimayank
Difference Between
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between IPv4 and IPv6
Difference Between Method Overloading and Method Overriding in Java
Stack vs Heap Memory Allocation
Differences between JDK, JRE and JVM
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Object Oriented Programming (OOPs) Concept in Java
Arrays.sort() in Java with examples