Home > Java > javaTutorial > Java getMethod()

Java getMethod()

PHPz
Release: 2024-08-30 15:39:49
Original
1071 people have browsed it

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 Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

getMethod() throws 3 types of exceptions as given below:-

  1. NoSuchMethodException
  2. NullPointerException
  3. SecurityException

Syntax

Below is the signature of the getMethod of java.lang.Class

public Method getMethod(String name, Class<?>... parameterTypes)
throws NoSuchMethodException, SecurityException
Copy after login
  • 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);
}
}
Copy after login

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);
Copy after login

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());
}
}
}
Copy after login

Output:

Java getMethod()

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";
}
Copy after login

Output:

Java getMethod()

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());
}
}
}
Copy after login

Output:

Java getMethod()

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.

The above is the detailed content of Java getMethod(). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template