Multiple ways to create objects in Java, please explain
Manohar Changed status to publish June 30, 2019
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