Enumerations
Enumeration in Java (or enum) is a special type in Java whose fields consist of constants. It is used to impose compile-time safety.
For example, consider the days of the week, they are a set of fixed constants, therefore we can have an enum defined:
public enum DayofWeek { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
Now we can simply check if a variable that stores a day is part of the declared enum. We can also declare enums for non-universal constants, such as:
public enum Jobs { DEVELOPER, TESTER, TEAM LEAD, PROJECT MANAGER }
This will enforce the job-type to be the constants declared in the Jobs enum. Here's an example enum holding currencies:
public enum Currency { USD, INR, DIRHAM, DINAR, RIYAL, ASD }
Exercise 33: Using Enum to Store Directions
We will create an enum and will find values and compare enums.
Create a class EnumExample and in the main method. Get and print the enum using the value as enum. Get and print enum using the values...