How many ways to create an object in java?

3.95K viewsJava
0

Multiple ways to create objects in Java, please explain

Manohar Changed status to publish June 30, 2019
Add a Comment
2

There are four ways we can create an object based on need, find the below:

1. By using a new keyword :
Employee  empObject = new Employee ();

2. By using clone() method :
Employee  secondObject= new Employee  ();
Employee  copyObject= (Employee  ) secondObject.clone();

3. By using Class.forName() :
Employee  object = (Employee  ) Class.forName(“com.rd.Employee”).newInstance();

4. By using object deserialization :

ObjectInputStream streamObject = new ObjectInputStream(anInputStream );
Employee  object = (Employee  ) inStream.readObject();

Note  : Employee  is a Class

Manohar Changed status to publish June 30, 2019
Add a Comment
Write your answer.