How do I create object using Constructor object?

The example below using a constructor reflection to create a string object by calling String(String) and String(StringBuilder) constructors.

package org.kodejava.lang.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class CreateObjectDemo {
    public static void main(String[] args) {
        Class<String> clazz = String.class;

        try {
            Constructor<String> constructor = clazz.getConstructor(String.class);

            String object = constructor.newInstance("Hello World!");
            System.out.println("String = " + object);

            constructor = clazz.getConstructor(StringBuilder.class);
            object = constructor.newInstance(new StringBuilder("Hello Universe!"));
            System.out.println("String = " + object);
        } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}

How do I get constructors of a class object?

Below is an example that showing you how to get constructors of a class object. In the code below we get the constructors by calling the Class.getDeclaredConstructors() or the Class.getConstructor(Class[]) method.

package org.kodejava.lang.reflect;

import java.lang.reflect.Constructor;

public class GetConstructors {
    public static void main(String[] args) {
        Class<String> clazz = String.class;

        // Get all declared constructors and iterate the constructors to get their
        // name and parameter types.
        Constructor<?>[] constructors = clazz.getDeclaredConstructors();
        for (Constructor<?> constructor : constructors) {
            String name = constructor.getName();
            System.out.println("Constructor name= " + name);

            Class<?>[] parameterTypes = constructor.getParameterTypes();
            for (Class<?> c : parameterTypes) {
                System.out.println("Param type name = " + c.getName());
            }
            System.out.println("----------------------------------------");
        }

        // Getting a specific constructor of the java.lang.String
        try {
            Constructor<String> constructor = String.class.getConstructor(String.class);
            System.out.println("Constructor     = " + constructor.getName());
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
}

How do I invoke a method using Method class?

This example demonstrate using reflection to invoke methods of a class object. Using reflection we can call method of an object using the given string name of the method. When using this method we need to catch for the NoSuchMethodException, IllegalAccessException and InvocationTargetException.

package org.kodejava.lang.reflect;

import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;

public class InvokingMethod {
    public static void main(String[] args) {
        InvokingMethod object = new InvokingMethod();
        Class<? extends InvokingMethod> clazz = object.getClass();

        try {
            // Invoking the add(int, int) method
            Method method = clazz.getMethod("add", int.class, int.class);
            Object result = method.invoke(object, 10, 10);
            System.out.println("Result = " + result);

            // Invoking the multiply(int, int) method
            method = clazz.getMethod("multiply", int.class, int.class);
            result = method.invoke(object, 10, 10);
            System.out.println("Result = " + result);

        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public int add(int numberA, int numberB) {
        return numberA + numberB;
    }

    public int multiply(int numberA, int numberB) {
        return numberA * numberB;
    }
}

How do I get the methods of a class object?

In this example we show you how to get the methods available in a class object. We show three ways to get the methods, they are:

  • Class.getDeclaredMethods()
  • Class.getMethods()
  • Class.getMethod(String, Class<?>...)
package org.kodejava.lang.reflect;

import java.lang.reflect.Method;

public class GetMethods {
    public static void main(String[] args) {
        GetMethods object = new GetMethods();
        Class<? extends GetMethods> clazz = object.getClass();

        // Get all declared methods including public, protected, private and
        // package (default) access but excluding the inherited methods.
        Method[] methods = clazz.getDeclaredMethods();
        for (Method method : methods) {
            System.out.println("Method name        = " + method.getName());
            System.out.println("Method return type = " + method.getReturnType().getName());

            Class<?>[] paramTypes = method.getParameterTypes();
            for (Class<?> c : paramTypes) {
                System.out.println("Param type         = " + c.getName());
            }

            System.out.println("----------------------------------------");
        }

        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

        // Get all methods including the inherited method. Using the getMethods()
        // we can only access public methods.
        methods = clazz.getMethods();
        for (Method method : methods) {
            System.out.println("Method name        = " + method.getName());
            System.out.println("Method return type = " + method.getReturnType().getName());

            Class<?>[] paramTypes = method.getParameterTypes();
            for (Class<?> c : paramTypes) {
                System.out.println("Param type         = " + c.getName());
            }

            System.out.println("----------------------------------------");
        }

        try {
            // We can also get method by their name and parameter types, here we
            // are trying to get the add(int, int) method.
            Method method = clazz.getMethod("add", int.class, int.class);
            System.out.println("Method name: " + method.getName());
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    public int add(int numberA, int numberB) {
        return numberA + numberB;
    }

    protected int multiply(int numberA, int numberB) {
        return numberA * numberB;
    }

    private double div(int numberA, int numberB) {
        return numberA / numberB;
    }
}