© Mikael Olsson 2022
M. OlssonJava 17 Quick Syntax Referencehttps://doi.org/10.1007/978-1-4842-7371-5_20

20. Enum

Mikael Olsson1  
(1)
Hammarland, Länsi-Suomi, Finland
 
An enumeration, or enum , is a type that consists of a fixed list of named constants. To create one, the enum keyword is used followed by a name and a code block containing a comma-separated list of constant elements. The access level for an enum is the same as for a class. Package-private by default, but it can also be set to public if it’s declared in a file of the same name. As with classes, an enum can also be contained within a class, where it can then be set to any access level.
enum Speed
{
  STOP, SLOW, NORMAL, FAST
}
An object of the enum type just shown can hold any one of the four defined constants. The enum constants are accessed as if they were static fields of a class.
Speed s = Speed.SLOW;
The switch statement provides a good example of when an enumeration can be useful. Compared to using ordinary constants, an enum has the advantage of allowing the programmer to clearly specify what constant values are allowed. This provides compile-time type safety. Note that when using an enum in a switch statement, the case labels aren’t qualified with the name of the enum.
public class MyApp
{
  public static void main(String args[]) {
    Speed s = Speed.NORMAL;
    // ...
    switch(s) {
      case STOP: break;
      case SLOW: break;
      case NORMAL: break;
      case FAST: break;
    }
  }
}

Enum Class

In Java, the enum type is more powerful than its counterparts in other languages, such as C++ or C#. Essentially a special kind of class, it can include anything a class can include. To add a class member, the list of constants must be terminated with a semicolon, and the member must be declared after the constants. In the following example, an integer is added to the enum, which will hold the actual speed that the elements represent.
enum Speed
{
  STOP, SLOW, NORMAL, FAST;
  public int velocity;
  // ...
}
To set this field, a constructor needs to be added as well. A constructor in an enum is always private and isn’t called in the same way as for a regular class. Instead, the parameters to the constructor are given after the constant elements, as seen in the next example. If a Speed enum object is assigned the constant SLOW, then the argument 5 will be passed to the constructor for that enum instance.
enum Speed
{
  STOP(0), SLOW(5), NORMAL(10), FAST(20);
  public int velocity;
  private Speed(int s) { velocity = s; }
}
public class MyApp
{
  public static void main(String args[]) {
    Speed s = Speed.SLOW;
    System.out.println(s.velocity); // "5"
  }
}
Another difference that enum types have when compared to regular classes is that they implicitly extend from the java.lang.Enum class. In addition to the members inherited from this class, the compiler will also automatically add two static methods to the enumeration, namely, values and valueof. The values method returns an array of the constant elements declared in the enum, and valueof returns the enum constant of the specified enum name.
Speed[] a = Speed.values();
String s = a[0].toString(); // "STOP"
Speed b = Speed.valueOf(s); // Speed.STOP
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.5.68