Enum:
It is a special data type used to define fixed set of constants . Enum can contain constants, methods etc. We can use enum with switch statement easily. Enum keyword is used to create enums.
Example:
class EnumDemo{
public enum Constant { Constant1, Constant2, Constant3, Constant4 }
public static void main(String[] args) {
for (Constant c : Constant.values())
System.out.println(c);
}}
It will output:
Output:Constant1
Constant2
Constant3
Constant4
Using Enum with Switch Statement:
class EnumDemo1{
enum Days{ SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY}
public static void main(String args[]){
Day days=Days.MONDAY;
switch(days){
case SUNDAY:
System.out.println("sunday");
break;
case MONDAY:
System.out.println("monday");
break;
default:
System.out.println("other day");
}
}}
It will output : monday
0 Comment(s)