Java Reflection class get constructors

Introduction

The following methods are available to retrieve constructors of a class:

public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)  
public Constructor<?>[] getDeclaredConstructors()  

public Constructor<T> getConstructor(Class<?>... parameterTypes)  
public Constructor<?>[] getConstructors()  
import java.lang.reflect.Constructor;

public class Main {
   public static void main(String args[]) throws Exception {
      Class c = Class.forName("java.lang.String");
      Constructor[] cons = c.getDeclaredConstructors();
      System.out.println("No of constructors in : " + cons.length);
      for (Constructor con : cons)
         System.out.println(con);
   }//from  w  ww  .ja v  a2 s . c o  m
}



PreviousNext

Related