There are four different
ways to create objects in java:
1.
Using new keyword:
This
is the most common way to create an object in java. Almost 99% of objects are
created in this way.
Customer customer = new Customer();
2
Using Class.forName() :
If we
know the name of the class & if it has a public default constructor we can
create an object in this way.
Customer customer = (Customer)
Class.forName("com.srinu.Customer").newInstance();
3. Using clone() :
The
clone() can be used to create a copy of an existing object.
Customer customer = new Customer();
Customer object = (Customer ) customer .clone();
4. Using object
deserialization :
Object
deserialization is nothing but creating an object from its serialized form.
ObjectInputStream
inStream = new ObjectInputStream(anInputStream );
Customer customer = (Customer) inStream.readObject();
No comments:
Post a Comment