Java Enums

Java Enums


Enumerations In Java
In its simplest form, an enumeration is a list of named constants that define a new data type.
  • An object of an enumeration type can hold only the values that are defined by the list.
  • Thus, an enumeration gives you a way to precisely define a new type of data that has a fixed number of valid values.
  • From a programming perspective, enumerations are useful whenever you need to define a set of values that represent a collection of items.
  • For example, you might use an enumeration to represent a set of status codes, such as success, waiting, failed, and retrying, which indicate the progress of some action. In the past, such values were defined as final variables, but enumerations offer a more structured approach.
The values( ) method produces an array of the enum constants in the order in which they were declared, so you can use the resulting array in (for example) a for-each loop. 
An enumeration is created using the enum keyword. For example, here is a simple enumeration that lists various forms of transportation.
Example - 
// This class is automatically inherited //from java.lang.Enum, which provides //certain capabilities that you can see in //this example-
//: enumerated/EnumClass.java
// Capabilities of the Enum class  
enum Shrubbery { GROUND, CRAWLING, HANGING }
public class EnumClass {
public static void main(String[] args) {
for(Shrubbery s : Shrubbery.values()) {
print(s + " ordinal: " + s.ordinal());
printnb(s.compareTo(Shrubbery.CRAWLING) + " ");
printnb(s.equals(Shrubbery.CRAWLING) + " ");
print(s == Shrubbery.CRAWLING);
print(s.getDeclaringClass());
print(s.name());
print("----------------------");
}
// Produce an enum value from a string name:
for(String s : "HANGING CRAWLING GROUND".split(" ")) {
Shrubbery shrub = Enum.valueOf(Shrubbery.class, s);
print(shrub);
}
}
}
Output -
GROUND ordinal: 0
-1 false false
class Shrubbery

GROUND
----------------------
CRAWLING ordinal: 1
0 true true
class Shrubbery
CRAWLING
----------------------
HANGING ordinal: 2
1 false false
class Shrubbery
HANGING
----------------------
HANGING
CRAWLING
GROUND

 
Enum In Switch Statements
We can use Enum with switch statements because -
A switch only works with an integral value, but since enums have an established integral order and the order of an instance can be produced with the ordinal( ) method, enums can be used in switch statements. 
  • Although normally you must qualify an enum instance with its type, you do not have to do this in a case statement.
  • Now we will understand with the help of an example -
Example-
package javaLearnings;

public class Main {
  public enum Day {

      MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
  }

  public static void main(String args[]) {

      Day[] daysOfWeek = Day.values();

      for (Day today : daysOfWeek) {

          //Using Enum in Switch case statement

          switch (today) {
              case MONDAY:
                  System.out.println("Today is Monday");
                  break;
              case TUESDAY:
                  System.out.println("Tuesday");
                  break;
              case WEDNESDAY:
                  System.out.println("Wednesday");
                  break;
              case THURSDAY:
                  System.out.println("Thursday");
                  break;
              case FRIDAY:
                  System.out.println("Friday");
                  break;
              case SATURDAY:
                  System.out.println("Saturday");
                  break;
              case SUNDAY:
                  System.out.println("Sunday");
                  break;

          }
      }
  }
}




 
Output -
Values() method in Enum
As noted earlier, all enum classes are created for you by the compiler and extend the Enum class. However, if you look at Enum, you’ll see that there is no values( ) method, even though we’ve been using it.
We will understand it using an example -
Example -
//: enumerated/UpcastEnum.java
// No values() method if you upcast an enum
enum Search { HITHER, YON }
public class UpcastEnum {
public static void main(String[] args) {
Search[] vals = Search.values();
Enum e = Search.HITHER; // Upcast
// e.values(); // No values() in Enum
for(Enum en : e.getClass().getEnumConstants())
System.out.println(en);
}
Output -
HITHER
YON 
NOTE -
We’ve established that all enums extend java.lang.Enum. Since Java does not support multiple inheritance, this means that you cannot create an enum via inheritance.