Different ways of getting a new object in Java
There are different ways of creating a new object in java . Few of them are explained as below :
1) new operator
2) reflection
3) deserialization
4) cloning
Using new operator :
It is the most common and regular way to create an object and a very simple one also. By using this method we can call whichever constructor we want to call (no-arg constructor as well as parameterized).
Using Reflection:
We can also use the newInstance() method of a Class class to create an object. This newInstance() method calls the no-arg constructor to create the object.
using Class.forName(“<fully-qualified-class-name>”)
Here code will compile without any issues, ie, with/without presence of MyClass.java in the classpath. But at runtime if the class is not found then it will throw run time exception. For this reason we are forced to handle ClassNotFoundException, it is an compile time exception, while loading a class at runtime
Using Clone method:
Whenever we call clone() on any object, the JVM actually creates a new object for us and copies all content of the previous object into it. Creating an object using the clone method does not invoke any constructor.
Here we should make sure MyClass.java is implementing Cloneable interface in order to clone its object.
Using Deserialization :
Whenever we serialize and deserialize an object, the JVM creates a separate object for us. In deserialization, the JVM doesn’t use any constructor to create the object.
Here we should make sure we are implementing Serializable interface if we wanna serialize/deserialize object.
1) new operator
2) reflection
3) deserialization
4) cloning
Using new operator :
It is the most common and regular way to create an object and a very simple one also. By using this method we can call whichever constructor we want to call (no-arg constructor as well as parameterized).
MyClass mc = new MyClass();
Using Reflection:
We can also use the newInstance() method of a Class class to create an object. This newInstance() method calls the no-arg constructor to create the object.
using Class.forName(“<fully-qualified-class-name>
Class c = Class.forName("com.codingfewer.MyClass"); c.newInstance();
Here code will compile without any issues, ie, with/without presence of MyClass.java in the classpath. But at runtime if the class is not found then it will throw run time exception. For this reason we are forced to handle ClassNotFoundException, it is an compile time exception, while loading a class at runtime
Using Clone method:
Whenever we call clone() on any object, the JVM actually creates a new object for us and copies all content of the previous object into it. Creating an object using the clone method does not invoke any constructor.
MyClass mc = new MyClass(); MyClass mcClone = mc.clone();
Here we should make sure MyClass.java is implementing Cloneable interface in order to clone its object.
Using Deserialization :
Whenever we serialize and deserialize an object, the JVM creates a separate object for us. In deserialization, the JVM doesn’t use any constructor to create the object.
ObjectInputStream ois = new ObjectInputStream(is); // Here is 'is' is stream representing the flat file // that is generated while serializing the object MyClass object = (MyClass) ois.readObject();
Here we should make sure we are implementing Serializable interface if we wanna serialize/deserialize object.
Comments
Post a Comment