GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Method Class | hashCode() Method in Java - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

Method Class | hashCode() Method in Java

Last Updated : 17 Jun, 2026

The hashCode() method of the java.lang.reflect. Method class returns a hash code value for a Method object. This hash code is computed using the method's name and its declaring class name. It is commonly used in hashing-based collections such as HashMap, HashSet, and Hashtable to efficiently store and retrieve method-related information.

  • Useful in collections such as HashMap and HashSet.
  • Returns the same hash code if the method object remains unchanged.
  • Works with Java Reflection API.

Syntax

public int hashCode()

Example 1: Get the hash code of a specific method object created by calling getDeclaredMethod() of Class object.

Java
import java.lang.reflect.Method;

public class GFG {

    public void getSampleMethod() {}

    public static void main(String[] args) {

        try {
            Class<?> c = GFG.class;

            Method method =
                c.getDeclaredMethod("getSampleMethod");

            int hashCode = method.hashCode();

            System.out.println(
                "hashCode of method "
                + method.getName()
                + " is "
                + hashCode);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Output
hashCode of method getSampleMethod is 1553813225

Explanation: In this example, the getDeclaredMethod() method is used to obtain the Method object representing getSampleMethod(). The hashCode() method is then called on this Method object to retrieve its hash code value. Finally, the program prints the method name along with its corresponding hash code.

Example 2: Program Demonstrate hashcode() method of Method Class.

Java
import java.lang.reflect.Method;

public class GFG {

    public void getSampleMethod() {}

    public String setSampleMethod() {
        return "hello India";
    }

    public static void main(String[] args) {

        try {
            Class<?> c = GFG.class;

            Method[] methods = c.getMethods();

            for (Method m : methods) {

                int hashCode = m.hashCode();

                System.out.println(
                    "hashCode of method "
                    + m.getName()
                    + " is "
                    + hashCode);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Output
hashCode of method main is 3282673
hashCode of method getSampleMethod is 1553813225
hashCode of method setSampleMethod is -1830532123
hashCode of method equals is -1918826964
hashCode of method toStri...

Explanation: Output of this program also showing results for method objects other than methods defined in class object like wait, equals, toString, hashCode, getClass, notify, and notifyAll, which are inherited from superclass Object of java.lang package by class Object.

Advantages of hashCode() Method

  • Enables efficient storage in hash-based collections.
  • Provides fast lookup and retrieval of method objects.
  • Helps compare method objects efficiently.
  • Works seamlessly with Java Reflection API.
  • Improves performance in large applications using reflection.
Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.