You are reading the article How Java Getmethod() Work With Sample Code updated in September 2023 on the website Chivangcangda.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 How Java Getmethod() Work With Sample Code
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.
Start Your Free Software Development Course
getMethod() throws 3 types of exceptions as given below:-
NoSuchMethodException
NullPointerException
SecurityException
SyntaxBelow is the signature of the getMethod of java.lang.Class
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.
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 chúng tôi 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 #1In 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.
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 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 #3In 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.
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:
ConclusionJava.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 ArticlesThis 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 –
You're reading How Java Getmethod() Work With Sample Code
Update the detailed information about How Java Getmethod() Work With Sample Code on the Chivangcangda.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!