- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Call methods of an object using reflection in Java
The methods of an object can be called using the java.lang.Class.getDeclaredMethods() method. This method returns an array that contains all the Method objects with public, private, protected and default access. However, the inherited methods are not included.
Also, the getDeclaredMethods() method returns a zero length array if the class or interface has no methods or if a primitive type, array class or void is represented in the Class object.
A program that demonstrates this is given as follows −
Example
import java.lang.reflect.Method; class ClassA { private String name = "John"; public String returnName() { return name; } } public class Demo { public static void main(String[] args) throws Exception { Class c = ClassA.class; Method[] methods = c.getDeclaredMethods(); ClassA obj = new ClassA(); for (Method m : methods) { Object result = m.invoke(obj, new Object[0]); System.out.println(m.getName() + ": " + result); } } }
Output
returnName: John
- Related Articles
- List methods of a class using Java Reflection
- Can we call methods using this keyword in java?
- PHP Call methods of objects in array using array_map?
- How to display methods and properties using reflection in C#?
- Can we call methods of the superclass from a static method in java?
- When can we call wait() and wait(long) methods of a Thread in Java?
- Initialize an Array with Reflection Utilities in Java
- Call append() method to construct a StringBuffer object in Java
- What are the methods of an array object in JavaScript?
- How to call custom methods in C#?
- How to call an interface method in Java?
- Create new instance of an Array with Java Reflection Method
- How to call another enum value in an enum's constructor using java?
- Using reflection to check array type and length in Java
- How to define methods for an Object in JavaScript?

Advertisements