🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The getDeclaredMethods() method in Java, part of the java.lang.Class class, is used to retrieve an array of Method objects representing all the methods declared by the class or interface represented by the Class object.
Table of Contents
- Introduction
getDeclaredMethods()Method Syntax- Understanding
getDeclaredMethods() - Examples
- Basic Usage
- Inspecting Method Details
- Real-World Use Case
- Conclusion
Introduction
The getDeclaredMethods() method returns an array of Method objects reflecting all the methods declared by the class or interface represented by the Class object, including private, protected, default (package) access, and public methods. This method does not return inherited methods.
getDeclaredMethods() Method Syntax
The syntax for the getDeclaredMethods() method is as follows:
public Method[] getDeclaredMethods() throws SecurityException
Parameters:
- This method does not take any parameters.
Returns:
- An array of
Methodobjects representing all the declared methods of the class or interface.
Throws:
SecurityException: If access to the method information is denied.
Understanding getDeclaredMethods()
The getDeclaredMethods() method allows you to retrieve all methods declared in a class, regardless of their access level. This includes private, protected, package-private, and public methods. It does not include methods inherited from superclasses.
Examples
Basic Usage
To demonstrate the basic usage of getDeclaredMethods(), we will create a class with different methods and retrieve all of them using this method.
Example
import java.lang.reflect.Method;
public class GetDeclaredMethodsExample {
public static void main(String[] args) {
Class<Person> personClass = Person.class;
Method[] methods = personClass.getDeclaredMethods();
for (Method method : methods) {
System.out.println("Method: " + method.getName());
}
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
private void privateMethod() {
System.out.println("Private method");
}
protected void protectedMethod() {
System.out.println("Protected method");
}
void packagePrivateMethod() {
System.out.println("Package-private method");
}
public void publicMethod() {
System.out.println("Public method");
}
}
Output:
Method: privateMethod
Method: protectedMethod
Method: packagePrivateMethod
Method: publicMethod
Inspecting Method Details
This example shows how to inspect additional details of each method, such as its return type and parameter types.
Example
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
public class InspectMethodDetailsExample {
public static void main(String[] args) {
Class<Person> personClass = Person.class;
Method[] methods = personClass.getDeclaredMethods();
for (Method method : methods) {
System.out.println("Method: " + method.getName());
System.out.println("Return type: " + method.getReturnType().getName());
Parameter[] parameters = method.getParameters();
for (Parameter parameter : parameters) {
System.out.println("Parameter: " + parameter.getName() + " of type " + parameter.getType().getName());
}
System.out.println();
}
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
private void privateMethod() {
System.out.println("Private method");
}
protected void protectedMethod() {
System.out.println("Protected method");
}
void packagePrivateMethod() {
System.out.println("Package-private method");
}
public void publicMethod() {
System.out.println("Public method");
}
}
Output:
Method: privateMethod
Return type: void
Method: protectedMethod
Return type: void
Method: packagePrivateMethod
Return type: void
Method: publicMethod
Return type: void
Real-World Use Case
Dynamic Method Invocation
In a real-world scenario, you might use the getDeclaredMethods() method to dynamically invoke methods of an object. This is particularly useful in frameworks that require runtime manipulation of objects, such as testing frameworks or dependency injection frameworks.
Example
import java.lang.reflect.Method;
public class DynamicMethodInvocationExample {
public static void main(String[] args) {
try {
Person person = new Person("Alice", 30);
Class<Person> personClass = Person.class;
Method[] methods = personClass.getDeclaredMethods();
for (Method method : methods) {
method.setAccessible(true); // Make the private methods accessible
if (method.getParameterCount() == 0) {
System.out.println("Invoking method: " + method.getName());
method.invoke(person);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
private void privateMethod() {
System.out.println("Private method");
}
protected void protectedMethod() {
System.out.println("Protected method");
}
void packagePrivateMethod() {
System.out.println("Package-private method");
}
public void publicMethod() {
System.out.println("Public method");
}
}
Output:
Invoking method: privateMethod
Private method
Invoking method: protectedMethod
Protected method
Invoking method: packagePrivateMethod
Package-private method
Invoking method: publicMethod
Public method
Conclusion
The Class.getDeclaredMethods() method in Java provides a way to retrieve all methods declared by a class, regardless of their access level. By using this method, you can dynamically access and manipulate methods of a class, making it particularly useful for reflection-based operations in frameworks and libraries.
Whether you are working with simple methods or handling complex dynamic method invocations, the getDeclaredMethods() method offers a reliable way to access and work with methods at runtime.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment