Method Class | hashCode() Method in Java

The java.lang.reflect.Method.hashCode() method returns the hash code for the Method class object. The hashcode returned is computed by exclusive-or operation on the hashcodes for the method’s declaring class name and the method’s name. The hashcode is always the same if the object doesn’t change. Hashcode is a unique code generated by the JVM at time of object creation. It can be used to perform some operation on hashing related algorithm like hashtable, hashmap etc. An object can also be searched with this unique code.

Syntax:

public int hashCode()

Returns: It returns an integer value which represents hashCode value for this Method.

Example:

Method: public void getvalue(){}
HashCode: 1553975225
Explanation: Hashcode is a unique code generated by the JVM at time of creation of the object
of Method getValue.when we going to apply hashCode function on method object of 
getValue it will return 1553975225 as hashCode.

Method:public void paint(){}
HashCode: 1643975341

Below program illustrates hashcode() method of Method class:
Program 1: Get the hash code of a specific method object created by calling getDeclaredMethod() of Class object.

filter_none

edit
close

play_arrow

link
brightness_4
code

/*
* Program Demonstrate hashcode() method of Method Class.
*/
import java.lang.reflect.Method;
  
public class GFG {
  
    // create a Method name getSampleMethod
    public void getSampleMethod() {}
  
    // create main method
    public static void main(String args[])
    {
  
        try {
  
            // create class object for class name GFG
            Class c = GFG.class;
  
            // get Method object of method name getSampleMethod
            Method method = c.getDeclaredMethod("getSampleMethod", null);
  
            // get hashcode of method object using hashCode() method
            int hashCode = method.hashCode();
  
            // Print hashCode with method name
            System.out.println("hashCode of method " + method.getName()
                               + " is " + hashCode);
        }
        catch (Exception e) {
            // print if any exception occures
            e.printStackTrace();
        }
    }
}

chevron_right


Output:

hashCode of method getSampleMethod is 1553813225

Program 2:

In this program after getting a list of Method objects of a class object by calling getMethods() method of class object, hashCode() method of Method object is called for each method object of list. At last the hashcode is printed along with method name.

filter_none

edit
close

play_arrow

link
brightness_4
code

/*
* Program Demonstrate hashcode() method of Method Class.
*/
import java.lang.reflect.Method;
  
public class GFG {
  
    // create a Method name getSampleMethod
    public void getSampleMethod() {}
  
    // create a Method name setSampleMethod
    public String setSampleMethod()
    {
  
        String str = "hello India";
        return str;
    }
  
    // create main method
    public static void main(String args[])
    {
  
        try {
  
            // create class object for class name GFG
            Class c = GFG.class;
  
            // get list of Method objects
            // of class object of gfg class
            Method[] methods = c.getMethods();
  
            // loop through methods list
            // and get hashcode of every method
            // and print those hashcode along with Method Name
            for (Method m : methods) {
  
                // get hashcode of current method of loop
                int hashCode = m.hashCode();
  
                // Print hashCode along with method name
                System.out.println("hashCode of method "
                                   + m.getName()
                                   + " is " + hashCode);
            }
        }
        catch (Exception e) {
            // print Exception if any Exception occurs.
            e.printStackTrace();
        }
    }
}

chevron_right


Output:

hashCode of method main is 3282673
hashCode of method getSampleMethod is 1553813225
hashCode of method setSampleMethod is -1830532123
hashCode of method wait is 1063184614
hashCode of method wait is 1063184614
hashCode of method wait is 1063184614
hashCode of method equals is -1918826964
hashCode of method toString is -1451283457
hashCode of method hashCode is 933549448
hashCode of method getClass is 1261057617
hashCode of method notify is -43061542
hashCode of method notifyAll is 1312178187

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.

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



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.