Java – Create object without ‘new’ keyword

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

Table of contents

Using Class.forName() and Class.newInstance()
ClassLoader loadClass()
Using Object.clone()
Deserialization
Using reflection

Note: In given examples, I am writing pseudo code only. For building complete fully working sample code, please read about related feature.

Create object using Class.newInstance()

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

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

Create object using class loader’s loadClass()

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

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

Create object using Object.clone()

This is also a way to have a new independent instance of a class.

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

Create new object using serialization and deserialization

If you have gone through this article, you can understand that serialization and de-serialization is also a way to have another instance of a class in system.

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

Create new object using reflection

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

constructor.newInstance(); or
class.newInstance();

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

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

16 thoughts on “Java – Create object without ‘new’ keyword”

  1. 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
  2. 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
  3. 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
    • 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

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.