How many ways to create an object in java?

2.30K viewsJava

Multiple ways to create objects in Java, please explain

Changed status to publish
Add a Comment

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

Changed status to publish
Add a Comment
You are viewing 1 out of 1 answers, click here to view all answers.
Write your answer.