Enum in Java
- Eclipse: Oxygen
- Java: 1.8
Enum is used to define a set of constants in java. Not only constant by it could also call any method, constructor associated with it. Which will be seen further in the tutorial.
We should always use enum when a variable (especially a method parameter) can only take one from a small set of possible values.
Enum is default public static hence we can directly reference variable by Enum Name.
You can define an enum type either independently or as part of a class. The code to define an enum independently is:
1 2 3 4 |
public enum Learning { COREJAVA,COLLECTIONS,GENERICS,JSPANDSERVLETS,MUILTITHREADING } |
This is an example to demonstrate the use of enums in switch-case statements.
Here we have a variable Learning of enum type, which is a collection of five constants COREJAVA, COLLECTIONS, GENERICS, JSPANDSERVLETS, MULTITHREADING.
The variable learn is a type of Learning (that is an enum type). This variable can take any value, out of the possible five values (COREJAVA, COLLECTIONS, GENERICS, JSPANDSERVLETS, MULTITHREADING). In this case, it is set to COREJAVA.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public class Demo { public static void main(String[] args) { Learning learn = Learning.COREJAVA; switch(learn){ case COLLECTIONS: System.out.println("Step 2 : Learning Collections framework"); break; case COREJAVA: System.out.println("Step 1 : learning Core Java"); break; case GENERICS: System.out.println("Step 3 : Learning Generics "); break; case JSPANDSERVLETS: System.out.println("Step 4 : Learning JSP and Servlets"); break; case MUILTITHREADING: System.out.println("Step 5 : Learning MuiltThreading"); break; default: System.out.println("Element not in the enumarated list"); break; } } } |
Output
Step 1: learning Core Java
The above concept is explained that enum can be used for more than one purpose, by the assignment of numbers to the enums and then access it by calling the original function.
1 2 3 4 5 6 7 8 9 10 11 |
public enum Learning { COREJAVA(1),COLLECTIONS(2),GENERICS(3),JSPANDSERVLETS(4),MUILTITHREADING(5); public int value; private Learning(int value) { this.value = value; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
public class Demo { public static void main(String[] args) { Learning learn = Learning.COREJAVA; switch(learn){ case COLLECTIONS: System.out.println("Step 2 : Learning Collections framework"); break; case COREJAVA: System.out.println("Step 1 : learning Core Java"); break; case GENERICS: System.out.println("Step 3 : Learning Generics "); break; case JSPANDSERVLETS: System.out.println("Step 4 : Learning JSP and Servlets"); break; case MUILTITHREADING: System.out.println("Step 5 : Learning MuiltThreading"); break; default: System.out.println("Element not in the enumarated list"); break; } for(Learning temp: Learning.values()){ System.out.println(temp.ordinal()); } } } |
Output
Step 1 : learning Core Java
0
1
2
3
4
Contributed by Poonam Tomar