Scale up as you grow — whether you're running one virtual machine or ten thousand.

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

@Pankaj Nice Article. One doubt. Assume if you’re getting an int, how would you map it to appropriate enum type. void someMethod(int i) { //how to map i to ThreadStatesEnum type? switch (th){ case START: System.out.println("START thread"); break; case WAITING: System.out.println("WAITING thread"); break; case RUNNING: System.out.println("RUNNING thread"); break; case DEAD: System.out.println("DEAD thread"); } } Regads.
- anonymous
Enum implicitly extends java.lang.Enum implements Comparable, Serializable, but not java.util.Enum which is mention most important points of Enum. Please make a not about that.
- raj
As it is written in API Enum.valueOf() doesn’t throw IllegalStateException. It throws IllegalArgumentException “if the specified enum type has no constant with the specified name, or the specified class object does not represent an enum type” instead
- Bektur Toktosunov
‘We can extend existing enum without breaking any existing functionality. For example, we can add a new field NEW in ThreadStates enum without impacting any existing functionality.’ Could you please explain this? How will we be adding new Field? Definitely we are not saying to edit the existing class. Right? Then how do we do that?
- Chandrika
Thank you for the good explanation and examples. Little remark. In the method “usingEnumMap()” instead “ThreadStates” we need use “ThreadStatesEnum”.
- Mykhailo
What do you mean by “We can extend existing enum without breaking any existing functionality. For example, we can add a new field NEW in ThreadStates enum without impacting any existing functionality.” Do you mean modify enum? I got confused by word “extend”. Do you mean to say something like enum “inheritance”?
- Raj
Hi Pankaj, I tried compiling the last program JavaEnumExamples. But I am getting 2 errors stating cannot convert Object to ThreadStateEnums. I have modified the program to make it work: package com.journaldev.enums; import java.io.IOException; import java.util.EnumMap; import java.util.EnumSet; import java.util.Set; public class JavaEnumExamples { public static void main(String[] args) throws IOException { usingEnumMethods(); usingEnumValueOf(); usingEnumValues(); usingEnumInSwitch(ThreadStatesEnum.START); usingEnumInSwitch(ThreadStatesEnum.DEAD); usingEnumMap(); usingEnumSet(); } public static void usingEnumMethods() throws IOException { ThreadStatesEnum thc = ThreadStatesEnum.RUNNING; System.out.println("Priority is: " +thc.getPriority()); System.out.println(“Overridden toString() method: " +thc.toString()); thc = ThreadStatesEnum.START; thc.setPriority(10); System.out.println(“Changed Priority Variable is: " +thc.getPriority()); System.out.println(“Overridden toString() method: " +thc.toString()); thc.close(); } public static void usingEnumValueOf() { ThreadStatesEnum th = Enum.valueOf(ThreadStatesEnum.class, “START”); System.out.println(“th priority: " +th.getPriority()); } public static void usingEnumValues() { ThreadStatesEnum[] thArray = ThreadStatesEnum.values(); for(ThreadStatesEnum th : thArray) { System.out.println(th.toString() +”::priority=” +th.getPriority()); } } public static void usingEnumInSwitch(ThreadStatesEnum th) { switch(th) { case START: System.out.println(“START thread”); break; case RUNNING: System.out.println(“Running Thread”); break; case WAITING: System.out.println(“Waiting Thread”); break; case DEAD: System.out.println(“Dead Thread”); break; } } public static void usingEnumMap() { EnumMap enumMap = new EnumMap(ThreadStatesEnum.class); enumMap.put(ThreadStatesEnum.START, “START”); enumMap.put(ThreadStatesEnum.RUNNING, “RUNNING”); enumMap.put(ThreadStatesEnum.WAITING, “WAITING”); enumMap.put(ThreadStatesEnum.DEAD, “DEAD”); Set keySet = enumMap.keySet(); for(Object keyT : keySet) { if(keyT instanceof ThreadStatesEnum) { ThreadStatesEnum key = (ThreadStatesEnum)keyT; System.out.println(“key=”+key.toString() +”::value=” +enumMap.get(key)); } } } public static void usingEnumSet() { EnumSet enumSet = EnumSet.allOf(ThreadStatesEnum.class); for(Object tsenumT : enumSet) { if(tsenumT instanceof ThreadStatesEnum) { ThreadStatesEnum tsenum = (ThreadStatesEnum)tsenumT; System.out.println("Using enumSet, priority = " +tsenum.getPriority()); } } } }
- Jay Vijay Liya