๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Method class in Reflection API  


Introduction
  • Method class in Java is part of the Reflection API and represents a single method of a class (either declared or inherited).
  • It is present in the java.lang.reflect package.
  • The Method class allows you to get information about, and invoke, methods of a class dynamically at runtime.
  • It provides methods to get metadata about a method, such as its name, return type, parameter types, modifiers, and also allows calling the method dynamically using invoke().
  • Method class inherits the Executable class and implements the GenericDeclaration and Member interfaces.
    Method class in Java Reflection

Important Methods of Method Class
  • Below are some important methods of Method class:-
  • S.No Method Use
    1 getName() Returns the name of the method.
    2 getReturnType() Returns the return type of the method as a Class object.
    3 getParameterTypes() Returns an array of Class objects representing the parameter types.
    4 getModifiers() Returns the Java language modifiers (public, private, static, etc.) as an int.
    5 invoke(Object obj, Object... args) Invokes the underlying method on the given object with specified arguments.
    6 isVarArgs() Checks if the method accepts a variable number of arguments.
  • Program:
    import java.lang.reflect.*;
    
    class Calculator
    {
        public int add(int a, int b)
        {
            return a + b;
        }
        private void display(String msg)
        {
            System.out.println("Message: " + msg);
        }
    }
    
    public class MainApp
    {
        public static void main(String[] args) throws Exception
        {
            Calculator calc = new Calculator();
            Class<?> c = Calculator.class;
    
            // Get declared methods
            for (Method method : c.getDeclaredMethods())
            {
                System.out.println("\nMethod Name: " + method.getName());
                System.out.println("Return Type: " + method.getReturnType().getSimpleName());
                System.out.println("Modifiers: " + Modifier.toString(method.getModifiers()));
    
                // Print parameter types
                Class<?>[] params = method.getParameterTypes();
                System.out.print("Parameter Types: ");
                for (Class<?> p : params)
                System.out.print(p.getSimpleName() + " ");
                System.out.println();
    
                // Access private methods
                method.setAccessible(true);
    
                // Dynamically invoke methods
                if (method.getName().equals("add"))
                {
                    Object result = method.invoke(calc, 10, 20);
                    System.out.println("Invoked Result: " + result);
                }
                else if (method.getName().equals("display"))
                {
                    method.invoke(calc, "Hello Reflection!");
                }
            }
        }
    }