Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Method Class | getModifiers() method in Java
Next article icon

Method Class | getName() Method in Java

Last Updated : 05 Dec, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report

The getName() method of java.lang.reflect.Method class is helpful to get the name of methods, as a String. To get name of all methods of a class, get all the methods of that class object. Then call getName() on those method objects.

Syntax:

public String getName()

Return Value: It returns the name of the method, as String.

Example:

  Method:public void getValue(){}  getName() returns: getValue  Explanation: The getName() function on object of above method returns name of method  which is getValue.    Method:public void paint(){}  getName() returns: paint  

Below programs illustrates getName() method of Method class:

Example 1: Print name of all methods of Method Object.




/*
* Program Demonstrate how to apply getName() method
* of Method Class.
*/
import java.lang.reflect.Method;
  
public class GFG {
  
    // Main method
    public static void main(String[] args)
    {
  
        try {
            // create class object
            Class classobj = GFG.class;
  
            // get list of methods
            Method[] methods = classobj.getMethods();
  
            // get the name of every method present in the list
            for (Method method : methods) {
  
                String MethodName = method.getName();
                System.out.println("Name of the method: "
                                   + MethodName);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
  
    // method name setValue
    public static int setValue()
    {
        System.out.println("setValue");
        return 24;
    }
  
    // method name getValue
    public String getValue()
    {
        System.out.println("getValue");
        return "getValue";
    }
  
    // method name setManyValues
    public void setManyValues()
    {
        System.out.println("setManyValues");
    }
}
 
 
Output:
  Name of the method: main  Name of the method: getValue  Name of the method: setValue  Name of the method: setManyValues  Name of the method: wait  Name of the method: wait  Name of the method: wait  Name of the method: equals  Name of the method: toString  Name of the method: hashCode  Name of the method: getClass  Name of the method: notify  Name of the method: notifyAll  

Example 2: Program to check whether class contains a certain specific method.




/*
* Program Demonstrate how to apply getName() method
* of Method Class within a class
*/
import java.lang.reflect.Method;
  
public class GFG {
  
    // Main method
    public static void main(String[] args)
    {
  
        String checkMethod = "method1";
  
        try {
            // create class object
            Class classobj = democlass.class;
  
            // get list of methods
            Method[] methods = classobj.getMethods();
  
            // get the name of every method present in the list
            for (Method method : methods) {
  
                String MethodName = method.getName();
                if (MethodName.equals(checkMethod)) {
                    System.out.println("Class Object Contains"
                                       + " Method whose name is "
                                       + MethodName);
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
// a simple class
class democlass {
  
    public int method1()
    {
        return 24;
    }
  
    public String method2()
    {
        return "Happy hours";
    }
  
    public void method3()
    {
        System.out.println("Happy hours");
    }
}
 
 
Output:
  Class Object Contains Method whose name is method1  

Reference:
https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#getName–



Next Article
Method Class | getModifiers() method in Java

A

AmanSingh2210
Improve
Article Tags :
  • Java
  • Java-Functions
  • java-lang-reflect-package
  • Java-Method Class
Practice Tags :
  • Java

Similar Reads

  • Method Class | getModifiers() method in Java
    The java.lang.reflect.Method.getModifiers() method of Method class returns the modifiers for the method represented by this Method object. It returns int value. Then with the use Modifier class, the name of modifiers corresponding to that value is fetched. Syntax: public int getModifiers() Return Va
    3 min read
  • Method Class | getAnnotation() method in Java
    The java.lang.reflect.Method.getAnnotation(Class< T > annotationClass) method of Method class returns Method objects' annotation for the specified type passed as parameter if such an annotation is present, else null. This is important method to get annotation for Method object. Syntax: public
    3 min read
  • Method Class | getReturnType() Method in Java
    Prerequisite : Java.lang.Class class in Java | Set 1, Java.lang.Class class in Java | Set 2The java.lang.reflectMethod Class help in getting information of a single method on a class or interface. This class also provides access to the methods of classes and invoke them at runtime. The getReturnType
    4 min read
  • Method Class | equals() Method in Java
    The java.lang.reflect.Method.equals(Object obj) method of Method class compares this Method Object against the specified object as parameter to equal(object obj) method. This method returns true if the Method object is the same as the passed object. Two Methods are the same if they were declared by
    3 min read
  • Method Class | getTypeParameters() Method in Java
    The java.lang.reflect.Method.getTypeParameters() method of Method class returns an array of TypeVariable objects declared by the generic declaration of this Method object, in declaration order. Elements of array represent the type variables objects declared by Method. An array of length 0 is returne
    4 min read
  • Method Class | getParameterCount() method in Java
    The java.lang.reflect.Method.getParameterCount() method of Method class returns the number of parameters declared on a method Object. Syntax: public int getParameterCount() Return Value: This method returns number of formal parameters defined on this Method Object. Below programs illustrates getPara
    3 min read
  • Method Class | getParameterTypes() Method in Java
    Prerequisite : Java.lang.Class class in Java | Set 1, Java.lang.Class class in Java | Set 2 java.lang.reflectMethod class help us to get information of a single method on a class or interface. This class also provides access to the methods of classes and invoke them at runtime. getParameterTypes() m
    4 min read
  • Method Class | getDefaultValue() Method in Java
    The getDefaultValue() method of java.lang.reflect.Method class. It returns the default value for the annotation member represented by the Method object. When annotation member belongs to primitive types, then the method returns the instance of that wrapper type. It returns null if annotation member
    3 min read
  • Method Class | getDeclaringClass() method in Java
    The java.lang.reflect.Method.getDeclaringClass() method of Method class returns the Class object for the class or interface which defines the method on which we are applying this getDeclaringClass() method. Syntax: public Class<?> getDeclaringClass() Return Value: This method returns the Class
    4 min read
  • Method Class | getExceptionTypes() Method in Java
    The java.lang.reflect.Method.getExceptionTypes() method of "Method class" returns an array of Exception Type Class Objects declared to be thrown by the method object to handle exception inside the method. All the exceptions handled by method using thrown clause, are returned as array of Class object
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences