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.
ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
getMethod() throws 3 types of exceptions as given below:-
Below is the signature of the getMethod of java.lang.Class
public Method getMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
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);
getMethod() returns a Method instance to the specified method in the referencing class or interface object.
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
3. NullPointerException: This is thrown if null is passed in place of methods name in the arguments.
Below are the examples mentioned:
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:
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:
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:
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.
The above is the detailed content of Java getMethod(). For more information, please follow other related articles on the PHP Chinese website!