Enumerations

An enumeration is a list of named constants. In java we have a capability of using enumerations as classes which expand it's features more extent.

Creating Enumerations

An enumeration is created by using enum keyword as follows:

//enum of days
enum Day
{
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
}
  • enum- It is a keyword to represent enums.

  • Day- It is the type of the enumeration where we are creating constants.

  • Sunday, Monday--- These are called as enumeration constants. These are implicitly declared as public static final member of type Day. These constants are called as self-typed, in which “self” refers to the enclosing enumeration.

Creating variable of type Enumeration

We can create a variable to an Enum same as variable for primitive data type although the definition of enum is like class but there is no need to instantiate the enum using new keyword.

//creating variable to an Enumeration of type Day

Day d;

Assigning value to variable of Enumeration

As d is the variable of enumeration of type Date so the values of type Date only can be assigned to the variable d as follows.

//assigning value to the variable of type Date
d = Day.Thursday

Using Enumerations

// An enumeration of Days.
enum Day 
{ 
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
}
public class EnumDemo 
{ 
public static void main(String args[]) 
{ 
Day d;
d = Day.Thursday; //we have to mention Day to access Day type value to assign it to a Day type variable

// Output an enum value. 
System.out.println("Value of d is:: " + d); 
System.out.println(); 

// Compare two enum values. 
if(d == Day.Friday) 
System.out.println("Day contains Friday"); 
else
System.out.println("Day does not contains Friday,it is "+d);

// Use an enum to control a switch statement. 
switch(d) 
{
 case Sunday: System.out.println("Day is Sunday");  //in case we are not using as Day.Sunday as in condition 
                    break;                          //we are mentioning d as Day type so it will compare automatically 
case Monday: System.out.println("Day is Monday"); 
                   break; 
case Tuesday: System.out.println("Day is Tuesday"); 
                    break;
case Wednesday: System.out.println( "Day is Wednesday"); 
                     break; 
case Thursday: System.out.println("Day is Thursday"); 
                     break; 
case Friday: System.out.println("Day is Friday"); 
                     break;
case Saturday: System.out.println("Day is Saturday"); 
                     break;  
} 
}
}

Output::

Value of d is:: Thursday

Day does not contains Friday,it is Thursday

Day is Thursday

The values( ) and valueOf( ) Methods

All enumerations are automatically contains these two methods as predefined methods whose definition is as follows

public static enum-type [ ] values( )

It returns the array of values that contains in a list of enumeration constants whose type is mentioned by enum-type like Date.

public static enum-type valueOf(String str )

It return the enumeration constant whose value is corresponding to the string parameter passed to the method where the type of enumeration mentioned by enum-type like Date.

//example for using values() and valueOf() methods
// An enumeration of Days.
enum Day 
{ 
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
}
public class Simple 
{ 
public static void main(String args[]) 
{ 
Day d[] = Day.values(); //initializes an array of Day type to store Day enumeration constants
String str= "Thursday";

//using values() method
System.out.println("using values() method");
for(Day i:d)
System.out.print(i +" ");
System.out.println();

//using  valueOf() method
System.out.println("using valueOf() method");
System.out.println(Day.valueOf(str));

System.out.println(Day.valueOf("Friday");
}
}

Output:

using values() method

Sunday Monday Tuesday Wednesday Thursday Friday Saturday

using valueOf() method

Thursday

Friday

Java Enumerations Are Class Types

Java Enumerations are like class even though we can't instantiate the variables(enum) of Enumeration type using new e yword but we can use constructors, instance variables and methods same like class.

When we creating a enum constant then the constructor will called so enum constant is like object to the enum type.

Each enum constant have its own copy of instance variables and methods.

//example for enum like class type
// An enumeration of Days.
enum Day 
{ 
Sunday(1), Monday(2), Tuesday, Wednesday(4), Thursday(5), Friday(6), Saturday(7); //while creating constant constructor will call automatically
private int num;
Day(int i)//overloading constructor with a paramter 
{
num=i;
}
Day()  //for Tuesday it will called as there is no parameter
{
num=-1;
}
int getNum() //returns the number which is defined while creating a constant
{
return num;
}
}
public class EnumDemo 
{ 
public static void main(String args[]) 
{ 
Day d[] = Day.values(); //initializes an array of Day type to store Day enumeration constants
System.out.println("using values() method");

for(Day i:d)
System.out.println("day is::"+i +" place in week is::"+i.getNum());
}
}

Output:

using values() method

day is::Sunday place in week is::1

day is::Monday place in week is::2

day is::Tuesday place in week is::-1

day is::Wednesday place in week is::4

day is::Thursday place in week is::5

day is::Friday place in week is::6

day is::Saturday place in week is::7

Enumerations Inherit Enum

While creating an Enum it will automatically inherit the Enum class as it is a class in lang package as

java.lang.Enum;

By inheriting Enum class, enumerations can use basic three methods as follows:

  • final int ordinal( )

By using this method we can get a position of a enumeration constant in a enum which is called as ordinal value which starts with 0 to total-1;.

  • final int compareTo(enum-type e)

By using this method we can comparetheordinal value of one constant with anothe renum constant of the same enumeration type.enum-type is the type of the enumeration and e is the constant of that type, this should be same type of calling constant.

If ordinal value is less than the constant passing to method then it will return negative value.

If both are same then it will return Zero.

If ordinal value is greater than the constant passed then it will return positive value.

  • boolean equals( )

This is the method override by the equals() method of an Object class. In enum we can compare one enumeration constant with any other object for equality by using this method which returns true if same other wise false we can do the same operation by using == operator.

//example for using methods from inherited Enum class
// An enumeration of Days.
enum Day 
{ 
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
}

public class EnumDemo
{ 
public static void main(String args[]) 
{ 
Day d[] = Day.values(); //initializes an array of Day type to store Day enumeration constants
Day d1= Day.Tuesday;
for(Day i:d)
System.out.println("day is::"+i +"It's ordinal value is::"+i.ordinal());
System.out.println();

System.out.println("using compareT0() method::"+d1.compareTo(Day.Friday));
System.out.println("using compareT0() method::"+d1.compareTo(Day.Tuesday));
System.out.println("using compareT0() method::"+d1.compareTo(Day.Sunday));

System.out.println();
System.out.println("using equals() method::"+d1.equals(Day.Tuesday));
System.out.println("using equals() method::"+d1.equals(Day.Friday));
}
}

Output:

day is::SundayIt's ordinal value is::0

day is::MondayIt's ordinal value is::1

day is::TuesdayIt's ordinal value is::2

day is::WednesdayIt's ordinal value is::3

day is::ThursdayIt's ordinal value is::4

day is::FridayIt's ordinal value is::5

day is::SaturdayIt's ordinal value is::6

using compareT0() method::-3

using compareT0() method::0

using compareT0() method::2

using equals() method::true

using equals() method::false

results matching ""

    No results matching ""