Passing a parameter to the constructor and calling a method dynamically : Constructor « Reflection « Java Tutorial






public class Main {
  public static void main(String args[]) throws Exception{
      String name = "java.lang.String";
      String methodName = "toLowerCase";

      Class cl = Class.forName(name);
      java.lang.reflect.Constructor constructor = cl.getConstructor(new Class[] { String.class });
      Object invoker = constructor.newInstance(new Object[] { "AAA" });
      Class arguments[] = new Class[] {};
      java.lang.reflect.Method objMethod = cl.getMethod(methodName, arguments);
      Object result = objMethod.invoke(invoker, (Object[]) arguments);
      System.out.println(result);
  }
}








7.3.Constructor
7.3.1.Get all constructors or by parameters
7.3.2.Demonstrates use of Constructor objects
7.3.3.A program that displays a class synopsis for the named class
7.3.4.Create new instance from Constructor
7.3.5.Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED
7.3.6.Load class with Class.forName
7.3.7.Call Private constructor
7.3.8.Get constructor by parameter type
7.3.9.Invoke a constructor which throws Exception
7.3.10.Passing a parameter to the constructor and calling a method dynamically
7.3.11.Getting a Constructor of a Class Object: By obtaining a list of all Constructors object
7.3.12.Getting a Constructor of a Class Object: By obtaining a particular Constructor object.
7.3.13.Get a compatible constructor for the given value type
7.3.14.Has Declared Constructor