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 !!
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.
Its underlying source code is like this: return new String(buf, true); you can see “new”
tell me more about this .
Hi Lokesh,
How to create object without new if my class is not cantain default constructor ?
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.
How about when we make the class as static and directly use its instance. Does this also counts under object creation without new keyword. ?
Absolutely yes.
wonderful query….
There are two reflective methods for creating instances of classes: java.lang.reflect.Constructor.newInstance() and Class.newInstance() https://docs.oracle.com/javase/tutorial/reflect/member/ctorInstance.html).
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?
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…
Hi Lokesh,
Your every approach use new keyword but you mention in heading,without new keyword
I disagree. Reason is that in few examples, I am using an existing object to create a new one e.g. clone(). Yes, that exiting object was created with new keyword.
final Value v = Value.class.getConstructor(int.class, int.class, double.class).newInstance(_xval1,_xval2,_pval);
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 ?
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.
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.