Java Language Reflection API Call overloaded constructors using reflection

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!


Example: Invoke different constructors by passing relevant parameters

import java.lang.reflect.*;

class NewInstanceWithReflection{
    public NewInstanceWithReflection(){
        System.out.println("Default constructor");
    public NewInstanceWithReflection( String a){
        System.out.println("Constructor :String => "+a);
    public static void main(String args[]) throws Exception {
        NewInstanceWithReflection object = (NewInstanceWithReflection)Class.forName("NewInstanceWithReflection").newInstance();
        Constructor constructor = NewInstanceWithReflection.class.getDeclaredConstructor( new Class[] {String.class});
        NewInstanceWithReflection object1 = (NewInstanceWithReflection)constructor.newInstance(new Object[]{"StackOverFlow"});


Default constructor
Constructor :String => StackOverFlow


  1. Create instance of class using Class.forName : It calls default constructor
  2. Invoke getDeclaredConstructor of the class by passing type of parameters as Class array
  3. After getting the constructor, create newInstance by passing parameter value as Object array

Got any Java Language Question?