Open In App
Related Articles

new Operator vs newInstance() Method in Java

Improve Article
Save Article
Like Article

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();



// 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
        // Printing elements of List


[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.  



// 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,
        // 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,
        // Calling above method over "A"


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

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to 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.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 02 Nov, 2022
Like Article
Save Article
Similar Reads
Complete Tutorials