In my previous article we have seen how to use Class.newInstance() method in creating object for class dynamically but there exist a problem the newInstance() method of Class class can invoke only no-arg constructor of the class when a class has parameterized constructors we cannot use newInstance() method of Class class at that place we need to go for the newInstance() method of the Constructor class. Here also we will load the class using Class.forName() method and get the class instance and get the constructor in the class. Lets see how we can achieve it.
Example of Constructor.newInstance() method
Lets now take a look into the below example for a better understanding.
Employee.java
Our Employee class is a simple Java concrete class with three attributes namely empId, empName, empSalary and their corresponding getters and setters.
public class Employee { private int empId; private String empName; private int empSalary; public Employee(int empId, String empName, int empSalary) { this.empId = empId; this.empName = empName; this.empSalary = empSalary; } public int getEmpId() { return empId; } public void setEmpId(int empId) { this.empId = empId; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } public int getEmpSalary() { return empSalary; } public void setEmpSalary(int empSalary) { this.empSalary = empSalary; } }
ConstructorNewInstanceExample.java
import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; public class ConstructorNewInstanceExample { public static void main(String args[]) { try { Class clasz = Class.forName("com.javainterviewpoint.Employee"); Constructor constructor = clasz.getConstructor(new Class[]{int.class,String.class,int.class}); Employee employee = (Employee)constructor.newInstance(1,"JavaInterviewPoint",45000); System.out.println("Employee Id : "+employee.getEmpId()); System.out.println("Employee Name : "+employee.getEmpName()); System.out.println("Employee Salary : "+employee.getEmpSalary()); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } }
The below line creates the object of type Class which encapsulates the Employee class.
Class clasz = Class.forName("com.javainterviewpoint.Employee);
We can get a particular constructor by calling getConstructor() method of clasz. Parameter passed should be of Class type matching the actual parameter of the Employee class. In Employee class we have one parameterized constructor taking int,string,int parameters
public Employee(int empId, String empName, int empSalary) { this.empId = empId; this.empName = empName; this.empSalary = empSalary; }
and hence we have passed int.class,String.class,int.class to the get that particular constructor.
Constructor constructor = clasz.getConstructor(new Class[]{int.class,String.class,int.class});
Finally newInstance() method of the Constructor class is called with parameters matching the constructor passed to get our Employee object.
Employee employee = (Employee)constructor.newInstance(1,"JavaInterviewPoint",45000);
When we run the above code we will get the below output.
Output:
Employee Id : 1 Employee Name : JavaInterviewPoint Employee Salary : 45000
What if more than one parameterized constructors are there?
When we have more than one constructor, lets see how we can pass-in the parameters dynamically and create objects. Lets make our Employee class to have more than one parameterized constructor.
Employee.java
Lets modify our Employee class to have 2 parameterized constructors. One constructor having three parameters (empId,empName,empSalary) and other having two parameters (empId,empName)
public class Employee { private static int empId; private String empName; private int empSalary; public Employee(int empId, String empName, int empSalary) { this.empId = empId; this.empName = empName; this.empSalary = empSalary; } public Employee(int empId, String empName) { this.empId = empId; this.empName = empName; } public int getEmpId() { return empId; } public void setEmpId(int empId) { this.empId = empId; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } public int getEmpSalary() { return empSalary; } public void setEmpSalary(int empSalary) { this.empSalary = empSalary; } }
ConstructorNewInstanceExample.java
import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; public class ConstructorNewInstanceExample { public static void main(String args[]) { try { Class clasz = Class.forName("com.javainterviewpoint.Employee"); Constructor[] allConstructors = clasz.getConstructors(); Class[] parametersOfConstructor1 = allConstructors[0].getParameterTypes(); Constructor constructor1 = clasz.getConstructor(parametersOfConstructor1); Employee employee1 = (Employee)constructor1.newInstance(1,"JavaInterviewPoint11"); System.out.println("***Employee1 Values***"); System.out.println("Employee Id : "+employee1.getEmpId()); System.out.println("Employee Name : "+employee1.getEmpName()); employee1.setEmpSalary(1111);//Setting salary since not set through constructor System.out.println("Employee Salary : "+employee1.getEmpSalary()); Class[] parametersOfConstructor2 = allConstructors[1].getParameterTypes(); Constructor constructor2 = clasz.getConstructor(parametersOfConstructor2); Employee employee2 = (Employee)constructor2.newInstance(2,"JavaInterviewPoint22",22222); System.out.println("***Employee2 Values***"); System.out.println("Employee Id : "+employee2.getEmpId()); System.out.println("Employee Name : "+employee2.getEmpName()); System.out.println("Employee Salary : "+employee2.getEmpSalary()); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } }
The below line creates Object of Class type
Class clasz = Class.forName("com.javainterviewpoint.Employee");
Now we get all the Constructors available in the Employee class using getConstructors() method, which returns array Constructor[]
Constructor[] allConstructors = clasz.getConstructors();
We can get the list of all parameter available for a particular constructor through constructor.getParameterTypes() method. It will be returning an array Class[]
Class[] parametersOfConstructor1 = allConstructors[0].getParameterTypes();
Get the particular Constructor by passing Parameter obtained in the previous step
Constructor constructor1 = clasz.getConstructor(parametersOfConstructor1);
Finally create object for our Employee class by calling constructor.newInstance() and passing the corresponding parameters.
Employee employee1 = (Employee)constructor1.newInstance(1,"JavaInterviewPoint11");
Output :
***Employee1 Values*** Employee Id : 1 Employee Name : JavaInterviewPoint11 Employee Salary : 1111 ***Employee2 Values*** Employee Id : 2 Employee Name : JavaInterviewPoint22 Employee Salary : 22222
Amol A. Ghadigaonkar says
Feeling absolutely grateful to learn this code from here…This made a lot of things easy for me…Thanks. 🙂