Java Interview Questions
In how many ways can you create an object in java?
There are 4 ways of creating objects in java.

1.Using new operator :

Employee obj=new Employee();

Here we are creating Employee class object ‘obj’ using new operator.

2.Using factory methods :

NumberFormat obj=NumberFormat.getNumberInstance();

Here, we are creating NumberFormat class object using factory method getNumberInstance().

3. Using newInstance() method. Here, we should follow two steps, as:

a.First, store the class name ‘Employee’ as a String into a object. For this purpose, factory method forName() of the class ‘Class’ will be useful.

Class c=Class.forName(“Employee”);

b.Next, create another object to the class whose name is in the object c. For this purpose, we need newInstance() method of the class ‘Class’, as:

Employee obj=(Employee)c.newInstance();

4.By cloning a already available object, we can create another object. Creating exact copy of a existing object is called ‘cloning’.

Employee obj1=new Employee();
Employee obj2=(Employee)obj1.clone();