12.1.3 Method Class Explained
The Method
class in Java SE 11 is a part of the Reflection API, which allows you to inspect and manipulate classes, fields, and methods at runtime. The Method
class specifically provides information about, and access to, a single method on a class or interface. Understanding the Method
class is crucial for advanced Java development, especially when dealing with dynamic behavior and runtime introspection.
Key Concepts
1. Reflection API
The Reflection API in Java allows you to examine or modify the runtime behavior of applications. It provides classes and interfaces that represent the classes, methods, fields, and constructors of a running Java application.
2. Method Class
The Method
class is a member of the java.lang.reflect
package. It represents a single method in a class or interface. You can use the Method
class to get information about the method's name, return type, parameters, annotations, and modifiers.
3. Accessing Methods
You can access methods of a class using the getMethods()
or getDeclaredMethods()
methods of the Class
class. The getMethods()
method returns all public methods, including inherited ones, while getDeclaredMethods()
returns all methods declared in the class, regardless of their access modifiers.
4. Invoking Methods
The Method
class provides the invoke()
method, which allows you to call the method represented by the Method
object. The invoke()
method takes the object on which to invoke the method and an array of arguments to pass to the method.
5. Method Parameters and Return Type
You can retrieve the parameters and return type of a method using the getParameterTypes()
and getReturnType()
methods of the Method
class. These methods return arrays of Class
objects representing the types of the parameters and the return type.
Examples and Analogies
Think of the Method
class as a key that unlocks the door to a specific method in a class. Just as a key can open a door to reveal what's inside, the Method
class can reveal details about a method and even execute it.
For example, if you have a class called Calculator
with a method add(int a, int b)
, you can use the Method
class to find out the name of the method, its parameters, and its return type. You can also use the Method
class to call the add
method dynamically.
Here's how you can use the Method
class to access and invoke the add
method:
import java.lang.reflect.Method; public class Calculator { public int add(int a, int b) { return a + b; } } public class ReflectionExample { public static void main(String[] args) { try { // Get the class object for Calculator Class<?> clazz = Class.forName("Calculator"); // Get the add method Method method = clazz.getMethod("add", int.class, int.class); // Create an instance of Calculator Calculator calculator = new Calculator(); // Invoke the add method Object result = method.invoke(calculator, 5, 3); // Print the result System.out.println("Result: " + result); // Output: Result: 8 } catch (Exception e) { e.printStackTrace(); } } }
In this example, the Method
class is used to find the add
method in the Calculator
class, and then the invoke
method is used to call the add
method with the arguments 5 and 3.
By mastering the Method
class, you can create more flexible and dynamic Java applications, capable of adapting to changing requirements and runtime conditions.