Fundamentals of Java Enum Types

Whatever code you’re working on, chances are you will sooner or later need a list of constants, like Monday, Tuesday, etc. for all weekdays or January, Februrary, etc. for all months. Java has you covered with enum types, usually only called enums, which make it exceedingly easy to define just that. In this article I will teach you everything you need to know to become a proficient user of Java enums.

Enum Fundamentals

In its simplest form a Java enum is just a fixed number of constants that the developer defines while writing the code. (It could, for example, define all SitePoint channels.) Much like a class, an enum defines a type that can be used just about anywhere where classes and interfaces could be used, for example for fields, variables, and parameters.

Enums can actually be much more than mere constants, having their own attributes and methods, but I will come to that later. Seeing them as constants is a good way to get to know them.

Defining an Enum

An enum is defined similarly to how a class would be defined but uses the enum instead of the class keyword. The constant values are listed in the enum’s body (meaning within the curly braces). While not necessary, it is common to write the constants in all uppercase letters to make it easier to recognize them.

Now that we have a description for enums, it’s time for an example. Here is what an enum looks like that lists some of the SitePoint channels:

public enum SitePointChannel {
    JAVA,
    MOBILE,
    WEB,
    PHP,
    WORDPRESS,
    JAVASCRIPT,
    DESIGN
}

The elements inside the SitePointChannel enum are called enumeration constants.

Assigning an Enum

Once an enum is defined, you can assign its values to variables. Here is an example:

private SitePointChannel channel;

The channel field is of type SitePointChannel, therefore the only values that it can be assigned are those defined by this enumeration. As a local variable that could look as follows:

SitePointChannel channel = SitePointChannel.JAVA;

Using an Enum

Enumerations have a variety of features in Java. One of their core capabilities is that they can be used in identity comparisons and switch statements.

Identity Comparisons With ==

Enumeration constants can be compared for identity by using the relational operator ==. Here is an example of an if() condition, which in this case is always false.

SitePointChannel channel = Sitepoint.JAVA;
if (channel == Sitepoint.MOBILE) {
    System.out.println("...");
}

Switch Statements

Another very widely used feature of enumerations is the ability to control switch statements. Here is an example that prints the content of a given channel:

SitePointChannel channel = ... // specify an enumeration constant
switch (channel) {
    case JAVA:
        System.out.println("Java, web and desktop technologies");
        break;
    case MOBILE:
        System.out.println("Mobile technologies");
        break;
    case PHP:
        // as usual for `switch`, one a match the execution
        // "falls through" to the next branch until it hits a break
    case WEB:
    case JAVASCRIPT:
    case WORDPRESS:
        System.out.println("Web technologies");
        break;
    default:
        throw new IllegalArgumentException(
                "Unknown channel '" + channel + "'.")
        break;
}

In switch statements, enumeration constants are used without their enumeration type name. This is due to the fact the enum type is implicitly specified in the switch expression. It is good practice to list all enum constants (even if some of them don’t do anything) and add a default branch, usually with an exception (in case a new constant gets added and someone misses the switch statement and doesn’t update it.)

Enums as a Class

With the basics of Java enums covered we can go beyond the interpretation of enums as a fixed number of constants. Enums are, in fact, much more like classes!

They can have fields and methods as well as implement interfaces and I will explain all of that in a minute. Even the enumeration constants are not that special – they are just public, static, and final members of their enum type. The only reason we don’t have to put public static final in is that the compiler fills it in for us.

The major differences towards regular classes is that they can not extend other classes (see below why) and can not be created with the new keyword (to keep the number of constants fixed).

Extending java.lang.Enum

Continue reading %Fundamentals of Java Enum Types%


Source: Sitepoint