Print out the contructors declared by String class with Reflection


import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;

public class Main {
  public static void main(String[] argv) throws Exception {
    Constructor[] constructors = String.class.getDeclaredConstructors();
    for (int i = 0; i < constructors.length; i++) {
      Constructor c = constructors[i];
      Class[] paramTypes = c.getParameterTypes();
      String name = c.getName();
      System.out.print(Modifier.toString(c.getModifiers()));
      System.out.print(" " + name + "(");
      for (int j = 0; j < paramTypes.length; j++) {
        if (j > 0)
          System.out.print(", ");
        System.out.print(paramTypes[j].getCanonicalName());
      }
      System.out.println(");");
    }
  }

}
  

The output:


public java.lang.String();
public java.lang.String(java.lang.String);
public java.lang.String(char[]);
public java.lang.String(char[], int, int);
public java.lang.String(int[], int, int);
public java.lang.String(byte[], int, int, int);
public java.lang.String(byte[], int);
public java.lang.String(java.lang.StringBuilder);
public java.lang.String(byte[], int, int, java.lang.String);
public java.lang.String(byte[], int, int, java.nio.charset.Charset);
public java.lang.String(byte[], java.lang.String);
public java.lang.String(byte[], java.nio.charset.Charset);
public java.lang.String(byte[], int, int);
public java.lang.String(byte[]);
public java.lang.String(java.lang.StringBuffer);
java.lang.String(int, int, char[]);
Home 
  Java Book 
    Reflection  

Constructor:
  1. Constructor Reflection
  2. Print out the contructors declared by String class with Reflection
  3. Create new instance by using its contructor