An enum is a special class that represents a group of constants like final variables. An enum is a special type of data type which is basically a collection/set of constants. Enum is short for enumerations which means specifically listed.
It can be used
Levels | LOW, MEDIUM, HIGH. |
---|---|
Directions | EAST, WEST, NORTH, SOUTH. |
Thread States | START, RUNNING, WAITING, DEAD. |
Season | SPRING, SUMMER, WINTER, AUTUMN, FALL. |
Size | SMALL, MEDIUM, LARGE, EXTRALARGE. |
Suits Playing Cards | CLUBS, DIAMONDS, HEARTS, SPADES. |
Colors | RED, YELLOW, BLUE, GREEN, WHITE, BLACK. |
Purchage | ORDERED, PICKING, PACKING, SHIPPING, READY, DELIVERED. |
Operators | ADDITION, SUBTRACTION, MULTIPLICATION, DIVISION, MODULUES. |
Days | SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY. |
Planet | MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE. |
Months | JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER. |
Java Enums Examples (Outside Enums)package com.jaladhi;import java.util.Scanner; public enum Days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } public class DaysTest { public static void main(String[] args) { Days days = Days.TUESDAY; switch(days) { case SUNDAY: System.out.println(days); break; case MONDAY: System.out.println(days); break; case TUESDAY: System.out.println(days); break; case WEDNESDAY: System.out.println(days); break; case THURSDAY: System.out.println(days); break; case FRIDAY: System.out.println(days); break; case SATURDAY: System.out.println(days); break; } } } Output :LOWMEDIUM |
package com.jaladhi; import java.util.Scanner; public enum Level { LOW, MEDIUM, HIGH } package com.jaladhi; import java.util.Scanner; public class LevelTest { public static void main(String[] args) { Level low = Level.LOW; System.out.println(low); Level medium = Level.MEDIUM; System.out.println(medium); } } Output :TUESDAY |
package com.jaladhi; import java.util.Scanner; public enum Thread { START, RUNNING, WAITING, DEAD; } package com.jaladhi; import java.util.Scanner; public class ThreadTest { public static void main(String[] args) { Thread start = Thread.START; System.out.println(start); Thread dead = Thread.DEAD; System.out.println(dead); } } Output :STARTDEAD |
package com.jaladhi; import java.util.Scanner; public enum Directions { EAST, WEST, NORTH, SOUTH } public class DirectionDemo { public static void main(String [ ] args){ Directions dir = Directions.NORTH; if(dir == Directions.EAST) { System.out.println("Direction: East"); } else if(dir == Directions.WEST) { System.out.println("Direction: West"); } else if(dir == Directions.NORTH) { System.out.println("Direction: North"); } else { System.out.println("Direction: South"); } } } |
Java Enums Example (Inside Enums)package com.jaladhi;import java.util.Scanner; public class ThreadTest { enum Thread { START, RUNNING, WAITING, DEAD; } public static void main(String[] args) { Thread start = Thread.START; System.out.println(start); Thread dead = Thread.DEAD; System.out.println(dead); } } Output :STARTDEAD |