Java – Create an Object without ‘new’ Keyword

We all know how to create objects of any class. The simplest method to create an object in Java is using new keyword. Let’s explore other methods to create objects without new keyword. 1. Using Class.newInstance() The Class.forName() loads the DemoClass in memory. To create an instance of …

We all know how to create objects of any class. The simplest method to create an object in Java is using new keyword. Let’s explore other methods to create objects without new keyword.

1. Using Class.newInstance()

Class ref = Class.forName("DemoClass");
DemoClass obj = (DemoClass) ref.newInstance();

The Class.forName() loads the DemoClass in memory. To create an instance of this class, we need to use newInstance().

2. Using ClassLoader

Just like the above method, the class loader’s loadClass() method does the same thing. It creates a new instance of a class using an existing instance of the same class.

instance.getClass().getClassLoader().loadClass("NewClass").newInstance();

3. Using Object.clone()

The clone() method is also a way to have a new independent instance of a class.

NewClass obj = new NewClass();
NewClass obj2 = (NewClass) obj.clone();

4. Using Serialization and Deserialization

If you have gone through serialization basics, you can understand that serialization and de-serialization are also a way to have another instance of a class in the runtime.

ObjectInputStream objStream = new ObjectInputStream(inputStream);
NewClass obj = (NewClass ) inStream.readObject();

5. Using Reflection

Reflection is also a popular way to create new instances in most of the available frameworks.

private <T> T instantiate(Class<?> input, Object parameter) {

  try {
    Constructor<?> constructor = ConstructorUtils
      .getMatchingAccessibleConstructor(input, parameter.getClass());

    return (T) constructor.newInstance(parameter);
  } catch (Exception e) {
    //handle various exceptions
  } 
}

If you think I am missing any other possible way, please let me know.

Happy Learning !!

Leave a Comment

  1. How will you make object of parameterized constructor using the newInstance method of Class class.
    Scenerio:
    class MyClass{
    MyClass(String methodType){
    System.out.println(“I am calling this constructor using ” + methodType);
    }
    }

    public class ABC{
    public static void main(String[] args) {
    //In below scenerio where I will pass my constructor’s parameter value
    MyClass meth2 = (MyClass) Class. forName(“packagePath.MyClass“).newInstance();
    }
    }

    Reply
  2. What about this scenario???

    String str1=new String(“Hello”);
    String str2=s1.concat(” World”);

    In this case we are not using any of the methods mentioned above right,but a new String object is getting created in String Constant Pool.

    Reply
    • Hi Mohan,

      There will be certain scenarios
      1. Does not have default constructor and having parameterised constructor.
      — In this case you will be using any of the ways mentioned above.
      2. Does not have any constructor.
      — JVM does the work on our behalf, by creating a default one. For this case also you have to use any of the above mentioned except the deserialisation and using ReflectionAPI.

      Reply
  3. How about when we make the class as static and directly use its instance. Does this also counts under object creation without new keyword. ?

    Reply
  4. There are two reflective methods for creating instances of classes: java.lang.reflect.Constructor.newInstance() and Class.newInstance() https://docs.oracle.com/en/java/javase/22/.

    So isn’t the first, second and last choice just point to same thing, i.e. getting instance of Class and running newInstance() method on it (of course in different ways). there by point to same thing that is reflection?

    Reply
  5. Using clone() of java.lang.Object part is wrong because the clone method is protected. You forget to write that we should implement our public instance of clone() in NewClass or super class of NewClass…

    Reply
  6. Hi Lokesh:

    Can you specify under what circumstances it is better to use each approach ??

    What would be the disadvantage of using the new keyword in some situation ?

    Reply
    • There are no advantages or disadvantages.. they are simply another ways to achieve this. I believe, I outcome is same, means matter a little less, if not at all.

      Also, you might know that most frameworks around us in java world use reflection or class loader or object creation, they seldom use new keyword.

      Reply
    • with new keyword , you are tightly coupling your code , whereas with the other techniques , you can make object depending on the some condition just by by passing the class name.

      Reply

Leave a Comment

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.