Facebook Page Twitter Page LinkedIn Page
× Java.lang Package Classes


The java.lang.Enum.hashCode() method is used to return a hash code for this enum constant.

Syntax

public final int hashCode()

Parameters

No parameter is required.

Return Value

Returns a hash code for this enum constant.

Exception

NA.

Example:

In the below example, the java.lang.Enum.hashCode() method is used to return a hash code for the given enum constant.

import java.lang.*;

public class MyClass {
  
  //creating an enum
  public enum weekday{
    MON, TUE, WED, THU, FRI
  }

  public static void main(String[] args) {
    
    //printing the hascode of enum constant
    System.out.print("hashCode of weekday.MON is: ");
    System.out.println(weekday.MON.hashCode());
    System.out.print("hashCode of weekday.WED is: ");
    System.out.println(weekday.WED.hashCode());
    System.out.print("hashCode of weekday.FRI is: ");
    System.out.println(weekday.FRI.hashCode());    
  }
}

The output of the above code will be:

hashCode of weekday.MON is: 1829164700
hashCode of weekday.WED is: 2018699554
hashCode of weekday.FRI is: 1311053135

❮ Java.lang - Enum