---
title: "new operator vs newInstance() method"  
description: "new operator vs newInstance() method"  
author: "Anonymous User"  
published: 2018-06-25  
updated: 2018-06-26  
canonical: https://www.mindstick.com/forum/34543/new-operator-vs-newinstance-method  
category: "java"  
tags: ["java", "class"]  
reading_time: 1 minute  

---

# new operator vs newInstance() method

new [operator](https://www.mindstick.com/blog/144/union-intersection-and-except-operator-in-sql-server) vs newInstance() [method](https://www.mindstick.com/forum/166/webservice-method) in [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming) ?

\

## Replies

### Reply by Prakash nidhi Verma

new operator vs newInstance() method in Java:

new operator is used to create objects but when you wants to create that in runtime thats no way you can use new operator. you have to use newInstance() method in this case.

// Java program to demonstrate working of newInstance()

```
class A {  int a; }
class B {  int b; }
public class Test
{
    public static void fun(String c)  throws InstantiationException,
        IllegalAccessException, ClassNotFoundException
    {
        Object obj = Class.forName(c).newInstance();
        System.out.println("Object created for class:"
                        + obj.getClass().getName());
    }
    public static void main(String[] args) throws InstantiationException,
    IllegalAccessException, ClassNotFoundException
    {
         fun("A");
    }
}
```

Class.forName() method return class object on which we are calling newInstance() method. If the class doesn’t exist then ClassNotFoundException will occur. InstantionException will occur if the class doesn’t receive any default constructor as newInstance() method.


---

Original Source: https://www.mindstick.com/forum/34543/new-operator-vs-newinstance-method

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
