We can use reflection to create the objects. Let’s say I have a class “Employee” in package p
Employee emp = Employee.class.newInstance() OR
Employee emp = (Employee)Class.forName("p.Employee").newInstance()
Above lines of code will create Employee object by calling Employee’s default or no-arg constructor. And then I set some values to objects by calling setters of Employee
Is there any way of creating objects using reflection by giving values in the constructor itself i.e. by calling parameterized constructor?
Take a look at Class.getConstructor and Constructor.
First you get the right constructor by its parameter types, then you call it with arguments of that type: