Get all constructors or by parameters in Java

Description

The following code shows how to get all constructors or by parameters.

Example


/*from w ww. j av a  2 s .  c o m*/
import java.lang.reflect.Constructor;

public class Main {

    public static void main(String[] args) {
        Constructor[] cs = String.class.getConstructors();
        for(int i=0;i<cs.length;i++){
            System.out.println(cs[i]);        
        }
        try {
            Constructor c = String.class.getConstructor(new Class[]{String.class});
            System.out.println(c); 
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Reflection »




Annotation
Array
Class
Constructor
Field
Generics
Interface
Method
Modifier
Package
Proxy