Enums

Enums, to put it simply, are categorized constants. For example, if you have a Direction enum, you might include NORTH, SOUTH, WEST, EAST. If you have a Days enum, you might include MONDAY, TUESDAY, WEDNESDAY, Thursday… By convention, constants and enums are all upper-case.

Below is the syntax for creating a enum object:

            public enum Direction{
                NORTH, SOUTH, EAST, WEST
            }
        

Simple right? There are more to enum but this is sufficient for now. Creating a new class gives you a new datatype, similarly, creating a new enum gives you a new datatype. For example, because we defined the Direction enum, Direction is now a datatype. To specify which constant you want to use in the enum, the syntax is <enum>.<constant>. Let’s do some example:

              Direction d1 = Direction.NORTH;
                Direction d2 = Direction.SOUTH;
                Direction d3 = Direction.NORTH
        

A few things to Note that there are no new keyword involved in the creation of enums, also, enums can be compared through ==, hence, d1 == d3 would give you true. That adds to our list of things we can compare with ==. As a review, we use == when we are comparing primitive types and enum.