Updated April 13, 2023
Introduction to Java getMethod()
Java getMethod() is a method in java.lang.Class.getMethod() that returns an instance of Method class in package java.lang.reflect that holds the reference of given public member function present in the given Class object reference to a class or interface. This method takes the name of the method required to be passed as its first parameter. The second parameter to be passed is an array of objects of Class that determine the formal parameter datatypes of the returned method or an empty array determining null as paramterTypes. The search algorithm used in this is the same as of private GetPublicMethods() method.
getMethod() throws 3 types of exceptions as given below:-
- NoSuchMethodException
- NullPointerException
- SecurityException
Syntax
Below is the signature of the getMethod of java.lang.Class
public Method getMethod(String name, Class<?>... parameterTypes)
throws NoSuchMethodException, SecurityException
- public: This keyword determines that the given method can be accessed from any class in the project.
- Return Type Method: This method returns an instance of Method class that refers to the required method whose name has been passed as arguments.
- Parameters:
- Name This parameter refers to the String representation of the name of the method present in the referencing class or interface object. If no such method is present in the class, NoSuchMethodException is raised. Otherwise, the algorithm runs, and the method is returned.
- parameterTypes: This refers to an array of Class-type objects that point to the data that the method in name parameter requires as arguments. The size of this array depends upon the arguments required by the specified method name. If the method requires no arguments, null is passed into this argument.
Examples
If we have a class Demo as given below:
class Demo{
public void method1(String a){
System.out.println(a);
}
}
Then call to getMethod would be like:
Demo demoObj= new Demo();// Object of Demo class
Class cObj= demoObj.getClass()
Class [] carr = new Class[1];
carr[0] = String.class;// class reference to java.lang.String class stored In the array of type Class
Method mObj = cObj.getMethod("method1",carr);
How getMethod() work in Java?
getMethod() returns a Method instance to the specified method in the referencing class or interface object.
- It takes a name parameter of String data type that holds the name of the public method that needs to be found in the specified class or interface. It also takes an array of Class Objects that represent the types of arguments for the function we are looking for.
- JVM reads the two arguments and performs a searching algorithm the same as used in the privateGetPublicMethods() method in java.lang.Class and search if the public method with the specified name is present or not. If there is more than one method is present in class, then the once with a more specific return type is returned. Otherwise, the method is chosen arbitrarily.
In case it finds the method, it returns an instance of Method Class holding its reference.
If the specified method does not need any argument, then null is passed in place of parameterType. This helps in the case of method overloading, where we have more than one method with the same name but differ in number or datatypes of arguments.This method throws 3 types of exceptions:-
1. NoSuchMethodException: This type of exception is thrown when JVM is not able to find any method with the specified name in class or interface.
2. SecurityException: This type of exception is thrown when
- checkMemberAccess(this, Member.PUBLIC) is invoked, denying access to it.
- The caller class load is different from the loader of the ancestor of the current class; thus, SecurityManagers.checkPackageAccess() is invoked; thus, access to the package is denied.
3. NullPointerException: This is thrown if null is passed in place of methods name in the arguments.
Examples to Implement Java getMethod()
Below are the examples mentioned:
Example #1
In this example, we will show the output of a getMethod call to two methods of Office class, one that requires objects and the other does not need an argument.
//package Proc;
import java.lang.reflect.*;
class Office{
public String OfficeLocation() {
return location;
}
public String getEmpName(Integer eid) {
return"Sergio";
}
String location = "Bangalore";
}
public class prac1 {
public static void main(String[] args) {
Office ofc = new Office();
Class cObj = ofc.getClass();
Class[] carr = new Class[1];
carr[0] = Integer.class;
try {
Method meth = cObj.getMethod("OfficeLocation", null);
System.out.println("Method with specified name is = " + meth.toString());
} catch(NoSuchMethodException e) {
System.out.println(e.toString());
}
try {
Method meth = cObj.getMethod("getEmpName", carr);
System.out.println("Method with specified name is = " + meth.toString());
} catch(NoSuchMethodException e) {
System.out.println(e.toString());
}
}
}
Output:
Example #2
In this example, we will see if JVM is able to find private methods is the class with the given name.
//package Proc;
import java.lang.reflect.*;
public class prac1 {
public static void main(String[] args) {
Office ofc = new Office();
Class cObj = ofc.getClass();
try {
Method meth = cObj.getMethod("OfficeLocation", null);
System.out.println("Method with specified name is = " + meth.toString());
} catch(NoSuchMethodException e) {
System.out.println(e.toString());
}
}
}
class Office{
private String OfficeLocation() {
return location;
}
public String getEmpName(Integer eid) {
return "Sergio";
}
String location = "Bangalore";
}
Output:
Example #3
In this example , we will see how different exceptions occur when a non-existing method is called, and null is passed in the method’s name.
//package Proc;
import java.lang.reflect.*;
class Office{
public String OfficeLocation() {
return location;
}
public String getEmpName(Integer eid) {
return "Sergio";
}
String location = "Bangalore";
}
public class prac1 {
public static void main(String[] args) {
Office ofc = new Office();
Class cObj = ofc.getClass();
Class[] carr = new Class[1];
carr[0] = Integer.class;
try {
Method meth = cObj.getMethod("show", null);
System.out.println("Method found " + meth.toString());
} catch(NoSuchMethodException e) {
System.out.println(e.toString());
}
try {
Method meth = cObj.getMethod(null, carr);
System.out.println("Method found" + meth.toString());
} catch(NoSuchMethodException e) {
System.out.println(e.toString());
}catch(NullPointerException e) {
System.out.println(e.toString());
}
}
}
Output:
Conclusion
Java.lang.getMethod() is a method used to search if a method with the given name and type of arguments is present in the class or not. It uses the same algorithm to find the method used in the privateGetPublicMethods() method. JVM search for the given public method and returns a Method instance; otherwise, NoSuchMethodException is raised.
Recommended Articles
This is a guide to Java getMethod(). Here we discuss an introduction to Java getMethod() with appropriate syntax, parameters, how does it work, and example. You can also go through our other related articles to learn more –