VOOZH about

URL: https://www.geeksforgeeks.org/java/method-class-hashcode-method-in-java/

⇱ Method Class | hashCode() Method in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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.


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.


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