All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Java Get Method Name

Last Updated : Mar 11, 2024

Java Get Method Name

In this article we will show you the solution of java get method name, this is accomplished by calling Class.getEnclosingMethod(), which returns a java.lang.reflect.Method object that contains almost every bit of information about the immediately enclosing method.

There is a significant overhead involved in this, since a new anonymous inner class is created behind the scenes.

An entity can be a class, interface, array, enum, method, etc., of a class object, and its name can be obtained by the getName() method of the class.

We will now discuss the idea of how to get method name in java with an example.

Step By Step Guide On Java Get Method Name :-

import java.lang.reflect.Method;
public class TalkersCode{
    public static void main(String[] args)
    {
        try {
            Class obj = ClassgetNameExample.class;
            Method[] method = obj.getMethods();
            for (Method method : method) {
                String MethodNm = method.getName();
                System.out.println("Name of the method: "
                                   + MethodNm);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static int setValueMethod()
    {
        System.out.println("setValueMethod");
        return 22;
    }
    public String getValueMethod()
    {
        System.out.println("getValueMethod");
        return "getValueMethod";
    }
    public void setManyValuesMethod()
    {
        System.out.println("setManyValuesMethod");
    }
}
  1. We import the Method class from the java.lang.reflect package.
  2. We define a public class called TalkersCode.
  3. A String array is passed as an argument to the main method of the TalkersCode class.
  4. We enclose the contents of the main method inside a try-catch block to handle any exceptions that might arise during program execution.
  5. We use the getClass() method to get a Class object that represents the TalkersCode class.
  6. We use the getMethods() method to get an array of Method objects that represent all the public methods of the TalkersCode class and its superclass, Object.
  7. We iterate over the array of Method objects using a for loop.
  8. Inside the for loop, we use the getName() method to get the name of each method in the array and store it in a String variable called MethodNm.
  9. We print the name of each method to the console using the System.out.println() method.
  10. We print a stack trace to the console if an exception is thrown during program execution.
  11. We define a public static method called setValueMethod() that returns an integer value and prints a message to the console.
  12. We define a public method called getValueMethod() that returns a String value and prints a message to the console.
  13. We define a public method called setManyValuesMethod() that returns void and prints a message to the console.

Conclusion :-

As a result, we have successfully learned how to get method name in java with example.

A dynamic method can be invoked at runtime using Java's reflection technology.

This can be useful in situations where you only know the name of the method once runtime or want to launch a method based on user input.

If you use reflection poorly, it can lead to a reduction in readability and maintainability of your code.

I hope this article on java get method name helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪