T21: Knowledge Tip Of The Day....
Different ways to create objects in Java
There are many different ways to create objects in Java.
1) Using new Keyword : Using new keyword is the most basic way
to create an object. This is the most common way to create an object in java.
example::
Demo obj = new Demo();
example::
Demo obj = new Demo();
2) Using New Instance : If we know the name of the class &
if it has a public default constructor we can create an object –Class.forName.
We can use it to create the Object of a Class. Class.forName actually loads the
Class in Java but doesn’t create any Object.
example::
Class cls = Class.forName("Demo");
Demo obj =
(Demo) cls.newInstance();
example::
Class cls = Class.forName("Demo");
Demo obj =
(Demo) cls.newInstance();
3) Using clone() method: Whenever clone() is called on any
object, the JVM actually creates a new object and copies all content of the
previous object into it. Creating an object using the clone method does not
invoke any constructor.
To use clone() method on an object we need to implement Cloneable and define the clone() method in it.
example::
Demo obj1=new Demo();
Demo obj2 = (Demo) obj1.clone();
Here we are creating the clone of an existing Object and not any new Object.
To use clone() method on an object we need to implement Cloneable and define the clone() method in it.
example::
Demo obj1=new Demo();
Demo obj2 = (Demo) obj1.clone();
Here we are creating the clone of an existing Object and not any new Object.
4) Using newInstance() method of Constructor class : This is
similar to the newInstance() method of a class. There is one newInstance()
method in the java.lang.reflect.Constructor class which we can use to create
objects. It can also call parameterized constructor, and private constructor by
using this newInstance() method.
Both newInstance() methods are known as reflective ways to
create objects. In fact newInstance() method of Class internally uses
newInstance() method of Constructor class.
example::
Constructor<ReflectionExample> constructor
= ReflectionExample.class.getDeclaredConstructor();
ReflectionExample r = constructor.newInstance();
example::
Constructor<ReflectionExample> constructor
= ReflectionExample.class.getDeclaredConstructor();
ReflectionExample r = constructor.newInstance();
5) Using deserialization : Whenever we serialize and then
deserialize an object, JVM creates a separate object. In deserialization, JVM
doesn’t use any constructor to create the object.
To deserialize an object we need to implement the Serializable interface in the class.
example::
Serializing an Object :
DeserializationExample d =
new DeserializationExample("Demo");
To deserialize an object we need to implement the Serializable interface in the class.
example::
Serializing an Object :
DeserializationExample d =
new DeserializationExample("Demo");
Deserialization of Object :
DeserializationExample d;
ObjectOutputStream oos = new ObjectOutputStream(f);
d = (DeserializationExample)oos.readObject
DeserializationExample d;
ObjectOutputStream oos = new ObjectOutputStream(f);
d = (DeserializationExample)oos.readObject
No comments:
Post a Comment