> For the complete documentation index, see [llms.txt](https://avbravo-2.gitbook.io/java-8/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://avbravo-2.gitbook.io/java-8/reflection/call-methods-at-runtime-using-java-reflection.md).

# Call Methods at Runtime Using Java Reflection

Fuente:

<https://www.baeldung.com/java-method-reflection>

Invocar metodos de la clase o una superclase

```java
```

## **Obtaining a*****Method*****Object**

First, we need to get a\_Method\_object that reflects the method we want to invoke. The\_Class\_object, representing the type in which the method is defined, provides two ways of doing this.

### **3.1.*****getMethod()***

We can use\_getMethod()\_to find any public method, be it static or instance that is defined in the class or any of its superclasses.

It receives the method name as the first argument, followed by the types of the method’s arguments:

| 123456 | `Method sumInstanceMethod= Operations.class.getMethod("publicSum",int.class,double.class);Method multiplyStaticMethod= Operations.class.getMethod("publicStaticMultiply",float.class,long.class);` |
| ------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

### **3.2.*****getDeclaredMethod()***

We can use\_getDeclaredMethod()\_to get any method defined in the class. This includes public, protected, default access, and even private methods but excludes inherited ones.

It receives the same parameters as*getMethod()*:

| 123 | `Method andPrivateMethod= Operations.class.getDeclaredMethod("privateAnd",boolean.class,boolean.class);` |
| --- | -------------------------------------------------------------------------------------------------------- |

| 12 | `Method maxProtectedMethod= Operations.class.getDeclaredMethod("protectedMax",int.class,int.class);` |
| -- | ---------------------------------------------------------------------------------------------------- |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://avbravo-2.gitbook.io/java-8/reflection/call-methods-at-runtime-using-java-reflection.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
