- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Get the declared method by name and parameter type in Java
The declared method can be obtained by name and parameter type by using the java.lang.Class.getDeclaredMethod() method. This method takes two parameters i.e. the name of the method and the parameter array of the method.
The getDeclaredMethod() method returns a Method object for the method of the class that matches the name of the method and the parameter array that are the parameters.
A program that gets the declared method by name and parameter type using the getDeclaredMethod() method is given as follows −
Example
package Test; import java.lang.reflect.*; public class Demo { public String str; private Integer func1() { return 1; } public void func2(String str) { this.str = "Stars"; } public static void main(String[] args) { Demo obj = new Demo(); Class c = obj.getClass(); try { Method m1 = c.getDeclaredMethod("func1", null); System.out.println("The method is: " + m1.toString()); Class[] argument = new Class[1]; argument[0] = String.class; Method m2 = c.getDeclaredMethod("func2", argument); System.out.println("The method is: " + m2.toString()); } catch(NoSuchMethodException e){ System.out.println(e.toString()); } } }
Output
The method is: private java.lang.Integer Test.Demo.func1() The method is: public void Test.Demo.func2(java.lang.String)
Now let us understand the above program.
In the class Demo, the two methods are func1() and func2(). A code snippet which demonstrates this is as follows −
public String str; private Integer func1() { return 1; } public void func2(String str) { this.str = "Stars"; }
In the method main(), an object obj is created of class Demo. Then getClass() is used to get the class of obj in c. Finally, the getDeclaredMethod() method is used to get the methods func1() and func2() and they are displayed. A code snippet which demonstrates this is as follows −
Demo obj = new Demo(); Class c = obj.getClass(); try { Method m1 = c.getDeclaredMethod("func1", null); System.out.println("The method is: " + m1.toString()); Class[] argument = new Class[1]; argument[0] = String.class; Method m2 = c.getDeclaredMethod("func2", argument); System.out.println("The method is: " + m2.toString()); }
- Related Articles
- Can a Java array be declared as a static field, local variable or a method parameter?
- Get the name of a primitive type in Java
- Get the list of all declared fields in Java
- Get the list of all the declared methods in Java
- Where exactly Type-Parameter need to be specified, while declaring generic method in Java?
- Get all declared fields from a class in Java
- Get count of zeros for columns values declared with INT type in MySQL
- Is it possible to instantiate Type-parameter in Java?
- method overloading and type promotion in Java
- Name the method by which we get salt from ocean water.
- Pass long parameter to an overloaded method in Java
- Get the type referenced by the specified type handle in C#
- Why is method overloading not possible by changing the return type of the method only in java?
- Can the overriding method throw the super-type of the exception thrown by the overridden method in Java?
- Get the name of the file and path in Java
