Java Enum:
Enum declaration Example 1:
Enum declaration Example 2:
4.Enum Fields
5.Enum Methods
Please click here Enum in java part 2
- In this tutorial I will explain what is enum, how to use enum in different areas of a Java program and an example program on it.
- An Enum type is a special data type which is added in Java 1.5 version. It is an abstract class in Java API which implements Cloneable and Serializable interfaces. It is used to define collection of constants. When you need a predefined list of values which do not represent some kind of numeric or textual data, at this moment, you have to use an enum.
- Common examples include days of week (values of SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY), directions, and months in a year. Etc.
- You can declare simple Java enum type with a syntax that is similar to the Java class declaration syntax. Let’s look at several short Java enum examples to get you started.
Enum declaration Example 1:
- public enum Day {
- SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
- }
Enum declaration Example 2:
- public enum Month{
- JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER,
- OCTOBER, NOVEMBER, DECEMBER
- }
- Enums are constants; they are by default static and final. That’s why the names of an enum type's fields are in uppercase letters.
- Make a note that the enum keyword which is used in place of class or interface. The Java enum keyword signals to the Java compiler that this type definition is an enum.
You can refer to the constants in the enum above like this:
- Day day = Day.MONDAY;
- Here the ‘day’ variable is of the type ‘Day’ which is the Java enum type defined in the example above. The ‘day’ variable can take one of the ‘Day’ enum constants as value (SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY). In this case ‘day’ is set to MONDAY.
- If you use enum instead of integers or String codes, you increase compile-time checking and avoid errors from passing in invalid constants, and you document which values are legal to use.
- How to use a Java enum type in various areas in a Java Program:
- We have seen how to declare simple Java enum types, let’s take a look at how to use them in various areas. We have to use a Java enum type in a variety of situations, including in a Java 5 for loop, in a switch statement, in an if/else statement, and more. For simple, Enum comparison, there are 3 ways to do it. They are, Switch-Case Statement, == Operator, .equals() method. Like that there are other places where you have to use Enum.
- Let's take a look at how to use our simple enum types with each of these Java constructs.
- Enum in IF statement
- Enum in Switch statement
- Enum Iteration in for-each loop
- Enum Fields
- Enum Methods
1. Enum in IF statements:
- We know Java Enums are constants. Sometimes, we have a requirement to compare a variable of Enum constant against the possible other constants in the Enum type. At this moment, we have to use IF statement as follows.
- Day day = ----- //assign some Day constants to it.
- If(day ==Day.MONDAY){
- ….//your code
- } else if (day == Day.TUESDAY){
- ….//your code
- } else if (day == Day.WEDNESDAY){
- ….//your code
- } else if (day == Day. THURSDAY){
- ….//your code
- } else if (day == Day.FRIDAY){
- ….//your code
- } else if (day == Day.SATURDAY){
- ….//your code
- } else if (day == Day.SUNDAY){
- ….//your code
- }
- Here, you can use “.equals()” instead of “==”. But, my preference is to use “==” because, it will never throws NullPointerException, safer at compile-time, runtime and faster.
- The code snippet compares the ‘day’ variable against each of the possible other Enum constants in the ‘Day’ Enum.
2. Enums in Switch Statements:
- Just assume that your Enum have lot of constants and you need to check a variable against the other values in Enum. For a small Enum IF Statement will be OK. If you use same IF statement for lot of Enum constants then our program will be increased and it is not a standard way to write a Java program. At this Situation, use Switch Statement instead of IF statement.
- You can use enums in switch statements as follows:
- Day day = ... //assign some Day constant to it
- switch (day) {
- case SUNDAY : //your code; break;
- case MONDAY //your code; break;
- case TUESDAY : //your code; break;
- case WEDNESDAY : //your code; break;
- case THURSDAY: //your code; break;
- case FRIDAY : //your code; break;
- case SATURDAY : //your code; break;
- }
- Here, give your code in the comments, “//your code”. The code should be a simple Java operation or a method call..etc
4.Enum Fields
5.Enum Methods
Please click here Enum in java part 2
No comments