Enum
An enum is a data type which
contains fixed set of constants. It can be used for days of the week (SUNDAY,
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY and SATURDAY) , directions (NORTH,
SOUTH, EAST and WEST) etc. The enum constants are static and final implicitely.
It is available from Java 5. Enums can be thought of as classes that have fixed
set of constants.
Points to remember for Enum:
- enum
improves type safety
- enum
can be easily used in switch
- enum
can be traversed
- enum
can have fields, constructors and methods
- enum
may implement many interfaces but cannot extend any class because it
internally extends Enum class
Simple
Example of enum in java:
1.
2. class EnumExample1{
3.
4. public enum Season { WINTER, SPRING, SUMMER, FALL }
5.
6. public static void main(String[] args) {
7. for (Season s : Season.values())
8. System.out.println(s);
9.
10. }}
11.
Output:WINTER
SPRING
SUMMER
FALL
What is
the purpose of values() method in enum?
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.
Internal
code generated by the compiler for the above example of enum type
The java compiler internally
creates a static and final class that extends the Enum class as shown in the
below example:
1. public static final class EnumExample1$Season extends Enum
2. {
3. private EnumExample1$Season(String s, int i)
4. {
5. super(s, i);
6. }
7.
8. public static EnumExample1$Season[] values()
9. {
10. return (EnumExample1$Season[])$VALUES.clone();
11. }
12.
13. public static EnumExample1$Season valueOf(String s)
14. {
15. return (EnumExample1$Season)Enum.valueOf(EnumExample1$Season, s);
16. }
17.
18. public static final EnumExample1$Season WINTER;
19. public static final EnumExample1$Season SPRING;
20. public static final EnumExample1$Season SUMMER;
21. public static final EnumExample1$Season FALL;
22. private static final EnumExample1$Season $VALUES[];
23.
24. static
25. {
26. WINTER = new EnumExample1$Season("WINTER", 0);
27. SPRING = new EnumExample1$Season("SPRING", 1);
28. SUMMER = new EnumExample1$Season("SUMMER", 2);
29. FALL = new EnumExample1$Season("FALL", 3);
30. $VALUES = (new EnumExample1$Season[] {
31. WINTER, SPRING, SUMMER, FALL
32. });
33. }
34.
35. }
Defining
enum:
The enum can be defined within
or outside the class because it is similar to a class.
Example
of enum that is defined outside the class:
1.
2. enum Season { WINTER, SPRING, SUMMER, FALL }
3.
4. class EnumExample2{
5. public static void main(String[] args) {
6.
7. Season s=Season.WINTER;
8. System.out.println(s);
9.
10. }}
11.
Output:WINTER
Example
of enum that is defined within the class:
1. class EnumExample3{
2. enum Season { WINTER, SPRING, SUMMER, FALL; }//semicolon(;) is optional here
3.
4. public static void main(String[] args) {
5. Season s=Season.WINTER;//enum type is required to access WINTER
6. System.out.println(s);
7.
8. }}
9.
Output:WINTER
Initializing
specific value to the enum constants:
The enum constants have initial
value that starts from 0, 1, 2, 3 and so on. But we can initialize the specific
value to the enum constants by defining fields and constructors. As specified
earlier, Enum can have fields, constructors and methods.
Example
of specifying initial value to the enum constants
1. class EnumExample4{
2. enum Season{
3. WINTER(5), SPRING(10), SUMMER(15), FALL(20);
4.
5. private int value;
6. private Season(int value){
7. this.value=value;
8. }
9. }
10. public static void main(String args[]){
11. for (Season s : Season.values())
12. System.out.println(s+" "+s.value);
13.
14. }}
Output:WINTER 5
SPRING 10
SUMMER 15
FALL 20
Constructor of enum type is
private if you don't declare private compiler internally have private
constructor
1. enum Season{
2. WINTER(10),SUMMER(20);
3. private int value;
4. Season(int value){
5. this.value=value;
6. }
7. }
8.
Internal
code generated by the compiler for the above example of enum type
1. final class Season extends Enum
2. {
3.
4. public static Season[] values()
5. {
6. return (Season[])$VALUES.clone();
7. }
8.
9. public static Season valueOf(String s)
10. {
11. return (Season)Enum.valueOf(Season, s);
12. }
13.
14. private Season(String s, int i, int j)
15. {
16. super(s, i);
17. value = j;
18. }
19.
20. public static final Season WINTER;
21. public static final Season SUMMER;
22. private int value;
23. private static final Season $VALUES[];
24.
25. static
26. {
27. WINTER = new Season("WINTER", 0, 10);
28. SUMMER = new Season("SUMMER", 1, 20);
29. $VALUES = (new Season[] {
30. WINTER, SUMMER
31. });
32. }
33. }
Can we
create the instance of enum by new keyword?
No,
because it contains private constructors only.
|
Can we
have abstract method in enum?
Yes, ofcourse! we can have
abstract methods and can provide the implementation of these methods.
Applying
enum on switch statement
We can apply enum on switch
statement as in the given example:
Example
of applying enum on switch statement
1. class EnumExample5{
2. enum Day{ SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY}
3.
4. public static void main(String args[]){
5.
6. Day day=Day.MONDAY;
7.
8. switch(day){
9. case SUNDAY:
10. System.out.println("sunday");
11. break;
12. case MONDAY:
13. System.out.println("monday");
14. break;
15. default:
16. System.out.println("other day");
17. }
18.
19. }}
Output:monday
No comments:
Post a Comment