Java Enums

In Java Programming enum keyword are used to define enumerated data type . That are used to create group of constant variable which are hold constant value. That can be defined by following syntax.

enum EnumName{identifier1,identifier2};

Enum name is user defined name. As naming convention their name is start with capital letter. And inside curly braces are define identifier names, that are separated by comma. That are define constant variables, So we can define name of identifier as capital letter (by naming conversion in java). Lets see examples.

//Enumeration of days name
enum Days{SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY};
//Enumeration of countries name
enum Countries{ CANADA, CHINA, INDIA, UNITESSTATES, BRAZIL};

This is basic example to define Enumerated elements. We can use those element by using of enum name. For example.

//Example Enum in Java
class Enumerated{
  //Enumeration of days name
  enum Days{SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY};

  public static void main(String args[]){
    //make variable of enum Days
    Days day=Days.MONDAY;
    System.out.println(day); //Display value
  }
}

Output

MONDAY

Note that in this example we are create a enumerated data variable by enum name. In this case Days day is a variable. And that value (Days.MONDAY) are assigned to variable.

That is an basic example which are provide enumeration variable. But in detail enumeration each element are create an instance which are contains two instance variable (name and ordinal).

//Example Enum in Java
public class MyEnum{
  //Enumeration of days name
  enum Iso{IN,CN,US,UK,JP};

  public static void main(String args[]){
    //Display class name
    System.out.println(Iso.IN.getClass().getName()); //MyEnum$Iso
    System.out.println(Iso.IN); //IN
    System.out.println(MyEnum.Iso.IN); //IN
  }
}
Java Enums Example
MyEnum$Iso
IN
IN

Note that there name and ordinal are private properties they are used by static public methods. There are various static method are available to perform enumeration operation. That are include few method in table.

Method Overview
public final int compareTo(E o) compare and find the distance of other enum objects
public final int hashCode() returning hashcode of enum object
public final String name() Returns the name of this enum constant,
public final int ordinal() returning the index value of identifier position
public final boolean equals(Object other) That is return true when argument is equal to calling object
public String toString(); Returning the name of this enum constant
value() Returns array of enum elements
//Example Enum in Java
class Enumerated{
  //Enumeration of days name
  enum Days{SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY};

  public static void main(String args[]){
    //make a object 
    Days day= Days.SUNDAY;

    //Position of  Days.SUNDAY  = 0 and  Days.THURSDAY = 4
    System.out.println(" compareTo : " +day.compareTo(Days.THURSDAY)); // (Days.SUNDAY-Days.THURSDAY) = -4

    System.out.println(" hashCode : "+day.hashCode()); //display hash code value

    System.out.println(" name  : "+day.name()); //display name
    //same as
    //System.out.println(day);

    System.out.println(" ordinal : "+Days.WEDNESDAY.ordinal()); //display index position

    System.out.println(" equals : "+Days.THURSDAY.equals(day)); //check equality and return boolean

     System.out.println(" toString : "+Days.SATURDAY.toString()); //convert to string

  }
}

Output

 compareTo : -4
 hashCode : 705927765
 name  : SUNDAY
 ordinal : 3
 equals : false
 toString : SATURDAY

In above example, compareTo() function are use to find interval (distance) of two enum objects. That function are return zero when both object are equal. otherwise position are subtracted first object to second object. And returns distance of object.

equals() function are check the equality of two enum objects. that result in boolean form when both object are same then that is returning true result. Otherwise returning false.

ordinal() function are returning position of object in integer value.

// Enum value method example
public class Enumerated{
  //Enumeration of days name
  enum Days{SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY};

  public static void main(String args[]){
    //values() function are returning  array of enum constant 
    //we can store this element into an array.
    // 
    Days []arr=Days.values();
    for(int i=0;i<arr.length;i++){
      System.out.println(arr[i]);
    }
    //Or
    for(Days name:Days.values()){
      System.out.println(name);
    }
  }
}
Enum value() method

Output

SUNDAY
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY

values() function returning the element of constant enum. And valueOf() function are accepted constant string, and returning the element of enum.

//Example Enum in Java
class Enumerated{
  //Enumeration of days name
  enum Days{SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY};

  public static void main(String args[]){
   //get index by constant name
  System.out.println(Days.valueOf("WEDNESDAY").ordinal());
  }
}

Output

3


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved