Field hashCode() method in Java with Examples Last Updated : 26 Aug, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report The hashCode() method of java.lang.reflect.Field used to get the hashcode for this Field. Final Hashcode is computed as the exclusive-or of the hashcodes for the underlying field's declaring class name and its name. The hashcode is always the same if the object doesn’t change. Hashcode is a unique code generated by the JVM at the time of object creation. It can be used to perform some operation on hashing related algorithm like a hashtable, hashmap etc. An object can also be searched with this unique code. Syntax: public int hashCode() Parameters: This method accepts nothing. Return value: This method returns an integer which is the hash code value for this object. Below programs illustrate hashCode() method: Program 1: Java // Java program to demonstrate hashCode() method import java.lang.reflect.Field; public class GFG { public static void main(String[] args) throws Exception { // Get the marks field object Field field = User.class.getField("Marks"); // Apply hashCode Method on User Object // to get the hashCode of Marks field int value = field.hashCode(); // print result System.out.println("HashCode of Marks field" + " is " + value); // Now Get the Fees field object field = User.class.getField("Fees"); // Apply hashCode Method on User Object // to get the hashcode of Fees field value = field.hashCode(); // print result System.out.println("HashCode of Fees field" + " is " + value); // Now Get the name field object field = User.class.getField("name"); // Apply hashCode Method on User Object // to get the hashCode of name field value = field.hashCode(); // print result System.out.println("HashCode of name field" + " is " + value); } } // sample User class class User { // static double values public static double Marks = 34.13; public static float Fees = 3413.99f; public static String name = "Aman"; public static double getMarks() { return Marks; } public static void setMarks(double marks) { Marks = marks; } public static float getFees() { return Fees; } public static void setFees(float fees) { Fees = fees; } public static String getName() { return name; } public static void setName(String name) { User.name = name; } } Output: HashCode of Marks field is 71482573 HashCode of Fees field is 591398 HashCode of name field is 1779040 Program 2: Java // Java program to demonstrate hashCode() method import java.lang.reflect.Field; import java.time.Month; public class GFG { public static void main(String[] args) throws Exception { // Get all field objects of Month class Field[] fields = Month.class.getFields(); for (int i = 0; i < fields.length; i++) { // print name of Fields System.out.println("HashCode of Field: " + fields[i].hashCode()); } } } Output: HashCode of Field: -297508095 HashCode of Field: 1296412905 HashCode of Field: 1475695976 HashCode of Field: 1343692077 HashCode of Field: 1404020238 HashCode of Field: 1401709321 HashCode of Field: 1401709395 HashCode of Field: 538235208 HashCode of Field: 2125827066 HashCode of Field: -1718938229 HashCode of Field: -1007182215 HashCode of Field: 59532142 References: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#hashCode-- Comment More infoAdvertise with us Next Article Field get() method in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java Java-Functions java-lang-reflect-package Java-Field Practice Tags : Java Similar Reads Float hashCode() method in Java with examples The hashCode() method method in Float Class is a built-in method in Java that return the hashcode value of this Float object. Syntax: public int hashCode() Parameters: It takes no parameters. Returns: The function returns a hash code value for this object. Below is the implementation of hashCode() m 1 min read FieldPosition hashCode() method in Java with Example The hashCode() method of java.text.FieldPosition class is used to get the hashcode of the FieldPosition object. Syntax: public int hashCode() Parameter: This method does not accepts any argument as parameter. Return Value: This method returns the hashcode of this FieldPosition object. Below are the 2 min read Field get() method in Java with Examples The get() method of java.lang.reflect.Field used to get the value of the field object. If Field has a primitive type then the value of the field is automatically wrapped in an object. If the field is a static field, the argument of obj is ignored; it may be null Otherwise, the underlying field is an 3 min read Byte hashCode() method in Java with examples The hashCode() method of Byte class is a built in method in Java which is used to return the hash code of the ByteObject. Note: The hashCode() returns the same value as intValue(). Syntax ByteObject.hashCode() Return Value: It return an int value which represents the hashcode of the specified Byte o 2 min read List hashCode() Method in Java with Examples This method is used to generate the hashCode for the given list. Implementation:Java// Java code to show the implementation of // hashCode method in list interface import java.util.*; public class Main { public static void main(String[] args) { // Initializing a list List<Integer> l = new Arra 2 min read Year hashCode() method in Java with Examples The hashCode() method of Year class in Java is used to get a suitable hash code for the current Year object. Syntax: public int hashCode() Parameter: This method does not accepts any parameter. Return Value: It returns a suitable integral hash code for the year object with which it is used. It never 1 min read Like