|
| 1 | +### Java Enums |
| 2 | + |
| 3 | +--------- |
| 4 | + |
| 5 | + |
| 6 | +The Enum in Java is a data type which contains a fixed set of constants. |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +It can be used for days of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY) , directions (NORTH, SOUTH, EAST, and WEST), season (SPRING, SUMMER, WINTER, and AUTUMN or FALL), colors (RED, YELLOW, BLUE, GREEN, WHITE, and BLACK) etc. According to the Java naming conventions, we should have all constants in capital letters. So, we have enum constants in capital letters. |
| 11 | + |
| 12 | + |
| 13 | +Java Enums can be thought of as classes which have a fixed set of constants (a variable that does not change). The Java enum constants are static and final implicitly. It is available since JDK 1.5. |
| 14 | + |
| 15 | + |
| 16 | +Enums are used to create our own data type like classes. The enum data type (also known as Enumerated Data Type) is used to define an enum in Java. Unlike C/C++, enum in Java is more powerful. Here, we can define an enum either inside the class or outside the class. |
| 17 | + |
| 18 | + |
| 19 | +Java Enum internally inherits the Enum class, so it cannot inherit any other class, but it can implement many interfaces. We can have fields, constructors, methods, and main methods in Java enum. |
| 20 | + |
| 21 | + |
| 22 | +------- |
| 23 | + |
| 24 | +##### Points to remember for Java Enum |
| 25 | +``` |
| 26 | +Enum improves type safety |
| 27 | +Enum can be easily used in switch |
| 28 | +Enum can be traversed |
| 29 | +Enum can have fields, constructors and methods |
| 30 | +Enum may implement many interfaces but cannot extend any class because it internally extends Enum class |
| 31 | +``` |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +Example |
| 36 | +```java |
| 37 | +class EnumExample1{ |
| 38 | +//defining the enum inside the class |
| 39 | +public enum Season { WINTER, SPRING, SUMMER, FALL } |
| 40 | +//main method |
| 41 | +public static void main(String[] args) { |
| 42 | +//traversing the enum |
| 43 | +for (Season s : Season.values()) |
| 44 | +System.out.println(s); |
| 45 | +}} |
| 46 | +``` |
| 47 | +Output |
| 48 | +``` |
| 49 | +WINTER |
| 50 | +SPRING |
| 51 | +SUMMER |
| 52 | +FALL |
| 53 | +``` |
| 54 | +----------- |
| 55 | + |
| 56 | +```java |
| 57 | +class EnumExample1{ |
| 58 | +//defining enum within class |
| 59 | +public enum Season { WINTER, SPRING, SUMMER, FALL } |
| 60 | +//creating the main method |
| 61 | +public static void main(String[] args) { |
| 62 | +//printing all enum |
| 63 | +for (Season s : Season.values()){ |
| 64 | +System.out.println(s); |
| 65 | +} |
| 66 | +System.out.println("Value of WINTER is: "+Season.valueOf("WINTER")); |
| 67 | +System.out.println("Index of WINTER is: "+Season.valueOf("WINTER").ordinal()); |
| 68 | +System.out.println("Index of SUMMER is: "+Season.valueOf("SUMMER").ordinal()); |
| 69 | + |
| 70 | +}} |
| 71 | +``` |
| 72 | +Output |
| 73 | +``` |
| 74 | +WINTER |
| 75 | +SPRING |
| 76 | +SUMMER |
| 77 | +FALL |
| 78 | +Value of WINTER is: WINTER |
| 79 | +Index of WINTER is: 0 |
| 80 | +Index of SUMMER is: 2 |
| 81 | +``` |
| 82 | + |
| 83 | +------ |
| 84 | +``` |
| 85 | +Q- What is the purpose of the values() method in the enum? |
| 86 | +A- The Java compiler internally adds the values() method when it creates an enum. The values() method returns an array containing all the values of the enum. |
| 87 | +
|
| 88 | +Q- What is the purpose of the valueOf() method in the enum? |
| 89 | +A- The Java compiler internally adds the valueOf() method when it creates an enum. The valueOf() method returns the value of given constant enum. |
| 90 | +
|
| 91 | +Q- What is the purpose of the ordinal() method in the enum? |
| 92 | +A- The Java compiler internally adds the ordinal() method when it creates an enum. The ordinal() method returns the index of the enum value. |
| 93 | +
|
| 94 | +``` |
| 95 | + |
| 96 | +-------- |
| 97 | + |
| 98 | +```java |
| 99 | +class EnumExample4{ |
| 100 | +enum Season{ |
| 101 | +WINTER(5), SPRING(10), SUMMER(15), FALL(20); |
| 102 | + |
| 103 | +private int value; |
| 104 | +private Season(int value){ |
| 105 | +this.value=value; |
| 106 | +} |
| 107 | +} |
| 108 | +public static void main(String args[]){ |
| 109 | +for (Season s : Season.values()) |
| 110 | +System.out.println(s+" "+s.value); |
| 111 | + |
| 112 | +}} |
| 113 | +``` |
| 114 | +Output |
| 115 | +``` |
| 116 | +WINTER 5 |
| 117 | +SPRING 10 |
| 118 | +SUMMER 15 |
| 119 | +FALL 20 |
| 120 | +``` |
| 121 | +-------- |
| 122 | + |
0 commit comments