Java Enum Tutorial: Ten Examples Of Enum Inward Java
What is Enum inward Java
Enum inward Java is a keyword, a characteristic which is used to stand upwards for fixed number of well-known values inward Java, For example, Number of days inward Week, Number of planets inward Solar organisation etc. Enumeration (Enum) inward Java was introduced inward JDK 1.5 in addition to it is 1 of my favorite features of J2SE v with Autoboxing in addition to unboxing , Generics, varargs in addition to static import. One of the mutual utilisation of Enum which emerged inward recent years is Using Enum to write Singleton inward Java, which is past times far easiest way to implement Singleton in addition to handles several issues related to thread-safety in addition to Serialization automatically. By the way, Java Enum every bit a type is to a greater extent than suitable to stand upwards for good known fixed laid of things in addition to state, for illustration representing the land of Order every bit NEW, PARTIAL FILL, FILL or CLOSED.
Enumeration(Enum) was non originally available inward Java though it was available inward some other linguistic communication similar C in addition to C++, but eventually, Java realized in addition to introduced Enum on JDK v (Tiger) past times keyword Enum.
In this Java Enum tutorial, nosotros volition run into different Enum illustration inward Java in addition to acquire using Enum inward Java. Focus of this Java Enum tutorial volition last on different features provided past times Enum inward Java in addition to how to utilisation them.
If you lot receive got used Enumeration earlier inward C or C++ thence you lot volition non last uncomfortable with Java Enum but inward my opinion, Enum inward Java is to a greater extent than rich in addition to versatile than inward whatsoever other language.
By the way, if you lot similar to acquire novel concepts using majority thence you lot tin too run into Head First Java sec Edition, I had followed this majority spell learning Enum, when Java 1.5 was get-go launched. This majority has splendid chapter non alone on Enum but too on key features of Java 1.5 and worth reading.
Though this tin serve our role it has some serious limitations:
1) No Type-Safety: First of all it’s non type-safe; you lot tin assign whatsoever valid int value to currency e.g. 99 though in that location is no money to stand upwards for that value.
2) No Meaningful Printing: printing value of whatsoever of these constant volition impress its numeric value instead of meaningful shout of money e.g. when you lot impress NICKLE it volition impress "5" instead of "NICKLE"
3) No namespace: to access the currencyDenom constant nosotros demand to prefix course of educational activity shout e.g. CurrencyDenom.PENNY instead of only using PENNY though this tin too last achieved past times using static import inward JDK 1.5
Java Enum is the reply of all this limitation. Enum inward Java is type-safe, provides meaningful String names in addition to has their ain namespace. Now let's run into the same illustration using Enum inward Java:
Here Currency is our enum in addition to PENNY, NICKLE, DIME, QUARTER are enum constants. Notice curly braces only about enum constants because Enum is a type similar class and interface inward Java. Also, nosotros receive got followed the similar naming convention for enum similar course of educational activity in addition to interface (first missive of the alphabet inward Caps) in addition to since Enum constants are implicitly static final nosotros receive got used all caps to specify them similar Constants inward Java.
Now dorsum to primary questions “What is Enum inward java” uncomplicated answer Enum is a keyword inward java in addition to on to a greater extent than item term Java Enum is a type similar course of educational activity in addition to interface in addition to tin last used to define a laid of Enum constants.
Enum constants are implicitly static in addition to final in addition to you lot tin non alter their value 1 time created. Enum inward Java provides type-safety in addition to tin last used within switch declaration similar int variables.
Since enum is a keyword you lot tin non utilisation every bit a variable shout in addition to since its alone introduced inward JDK 1.5 all your previous code which has an enum every bit a variable shout volition non operate in addition to needs to last refactored.
1) Enum is type-safe you lot tin non assign anything else other than predefined Enum constants to an Enum variable. It is a compiler fault to assign something else, dissimilar earth static in conclusion variables used inward Enum int pattern in addition to Enum String pattern.
2) Enum has its ain namespace.
3) The best characteristic of Enum is you tin utilisation Enum inward Java within Switch statement similar int or char primitive information type. We volition too run into an illustration of using coffee enum inward switch statement inward this coffee enum tutorial.
4) Adding novel constants on Enum inward Java is slow in addition to you lot tin add together novel constants without breaking the existing code.
2) Enum inward Java are reference types like class or interface and you lot tin define constructor, methods in addition to variables within coffee Enum which makes it to a greater extent than powerful than Enum inward C in addition to C++ every bit shown inward adjacent illustration of Java Enum type.
3) You tin specify values of enum constants at the creation time every bit shown inward below example:
But for this to operate you lot demand to define a fellow member variable in addition to a constructor because PENNY (1) is genuinely calling a constructor which accepts int value, run into below example.
The constructor of enum inward java must last private any other access modifier volition lawsuit inward compilation error. Now to acquire the value associated with each money you lot tin define a world getValue() method within Java enum similar whatsoever normal Java class. Also, the semicolon inward the get-go trouble is optional.
4) Enum constants are implicitly static and final and tin non last changed 1 time created. For example, below code of coffee enum volition lawsuit inward compilation error:
The in conclusion land EnumExamples.Currency.PENNY cannot last reassigned.
5) Enum inward coffee tin last used every bit an declaration on switch statement in addition to with "case:" similar int or char primitive type. This characteristic of coffee enum makes them rattling useful for switch operations. Let’s run into an illustration of how to utilisation coffee enum within switch statement:
from JDK vii onwards you lot tin too String inward Switch instance inward Java code.
6) Since constants defined within Enum inward Java are in conclusion you lot tin safely compare them using "==", the equality operator every bit shown inward next illustration of Java Enum:
By the way comparison objects using == operator is non recommended, Always utilisation equals() method or compareTo() method to compare Objects.
If you lot are non convinced than you lot should read this article to acquire to a greater extent than nearly pros in addition to cons of comparison 2 enums using equals() vs == operator inward Java.
7) Java compiler automatically generates static values() method for every enum inward java. Values() method returns array of Enum constants inward the same lodge they receive got listed inward Enum in addition to you lot tin utilisation values() to iterate over values of Enum inward Java every bit shown inward below example:
And it volition print:
Notice the lodge is just the same as defined lodge inward the Enum.
8) In Java, Enum tin override methods also. Let’s run into an illustration of overriding toString() method inside Enum inward Java to provide a meaningful description for enums constants.
And hither is how it looks similar when displayed:
9) Two novel collection classes EnumMap and EnumSet are added into collection packet to support Java Enum. These classes are a high-performance implementation of Map in addition to Set interface inward Java and nosotros should utilisation this whenever in that location is whatsoever opportunity.
EnumSet doesn't receive got whatsoever world constructor instead it provides mill methods to create instance e.g. EnumSet.of() methods. This blueprint allows EnumSet to internally pick out betwixt 2 different implementations depending upon the size of Enum constants.
If Enum has less than 64 constants than EnumSet uses RegularEnumSet course of educational activity which internally uses a long variable to shop those 64 Enum constants in addition to if Enum has to a greater extent than keys than 64 thence it uses JumboEnumSet. See my article the difference betwixt RegularEnumSet in addition to JumboEnumSet for to a greater extent than details.
10) You tin non create an instance of enums past times using novel operator inward Java because the constructor of Enum inward Java tin alone last private in addition to Enums constants tin alone last created within Enums itself.
11) An instance of Enum inward Java is created when whatsoever Enum constants are get-go called or referenced inward code.
12) Enum inward Java tin implement the interface in addition to override whatsoever method similar normal course of educational activity It’s too worth noting that Enum inward coffee implicitly implements both Serializable and Comparable interface. Let's run into in addition to illustration of how to implement interface using Java Enum:
13) You tin define abstract methods within Enum inward Java in addition to tin too provide a different implementation for different instances of enum inward java. Let’s run into an example of using abstract method within enum inward java
In this illustration since every money volition receive got the different color nosotros made the color() method abstract in addition to allow each instance of Enum to define their ain color. You tin acquire color of whatsoever money past times only calling the color() method every bit shown inward below illustration of Java Enum:
So that was the comprehensive listing of properties, behaviour in addition to capabilities of Enumeration type inward Java. I know, it's non slow to recollect all those powerful features in addition to that's why I receive got prepared this minor Microsoft powerpoint slide containing all of import properties of Enum inward Java. You tin ever come upwards dorsum in addition to depository fiscal establishment check this slide to revise of import features of Java Enum.
I think some existent basis examples of enum volition practise a lot of skillful to many pepole in addition to that's why I am going to summarize some of the pop usage of Enum inward Java basis below.
Enum every bit Thread Safe Singleton
One of the most pop utilisation of Java Enum is to impelment the Singleton blueprint pattern inward Java. In fact, Enum is the easieset way to create a thread-safe Singleton inward Java. It offering thence many payoff over traditional implementation using course of educational activity e.g. built-in Serialization, guarantee that Singleton volition ever last Singleton in addition to many more. I advise you lot to depository fiscal establishment check my article nearly Why Enum every bit Singelton is ameliorate inward Java to larn to a greater extent than on this topic.
Strategy Pattern using Enum
You tin too implement the Strategy blueprint pattern using Enumeration type inward Java. Since Enum tin implement interface, it's a skillful candidate to implement the Strategy interface in addition to define private strategy. By keeping all related Strategy inward 1 place, Enum offering ameliorate maintainence support. It too doesn't intermission the opened upwards shut blueprint regulation every bit per se because whatsoever fault volition last detected at compile time. See this tutorial to acquire how to implement Strategy pattern using Enum inward Java.
Enum every bit replacement of Enum String or int pattern
There is at 1 time no demand to utilisation String or integer constant to stand upwards for fixed laid of things e.g. condition of object similar ON in addition to OFF for a push or START, IN PROGRESS in addition to DONE for a Task. Enum is much ameliorate suited for those needs every bit it provide compile fourth dimension type security in addition to ameliorate debugging assistent than String or Integer.
Enum every bit State Machine
You tin too utilisation Enum to impelment State machine inward Java. Influenza A virus subtype H5N1 State machine transition to predifine laid of states based upon electrical current land in addition to given input. Since Enum tin implement interface in addition to override method, you lot tin utilisation it every bit State machine inward Java. See this tutorial from Peter Lawrey for a working example.
Enum Java valueOf example
One of my readers pointed out that I receive got non mentioned nearly the valueOf method of enum inward Java, which is used to convert String to enum inward Java.
Here is what he has suggested, thank you lot @ Anonymous
Another of my reader suggested nearly ordinal() in addition to name() utility method of Java enum Ordinal method of Java Enum returns the seat of a Enum constant every bit they declared inward enum spell name()of Enum returns the exact string which is used to create that exceptional Enum constant.” name() method tin too last used for converting Enum to String inward Java.
That’s all on Java enum, Please part if you lot receive got whatsoever overnice tips on enum inward Java in addition to allow us know how you lot are using coffee enum inward your work. You tin too follow some skillful advice for using Enum past times Joshua Bloch inward his all fourth dimension classic majority Effective Java. That advice volition give you lot to a greater extent than thought of using this powerful characteristic of Java programming language
Some Java Tutorials you lot May Like The existent divergence betwixt EnumMap in addition to HashMap inward Java
Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!
Enum inward Java is a keyword, a characteristic which is used to stand upwards for fixed number of well-known values inward Java, For example, Number of days inward Week, Number of planets inward Solar organisation etc. Enumeration (Enum) inward Java was introduced inward JDK 1.5 in addition to it is 1 of my favorite features of J2SE v with Autoboxing in addition to unboxing , Generics, varargs in addition to static import. One of the mutual utilisation of Enum which emerged inward recent years is Using Enum to write Singleton inward Java, which is past times far easiest way to implement Singleton in addition to handles several issues related to thread-safety in addition to Serialization automatically. By the way, Java Enum every bit a type is to a greater extent than suitable to stand upwards for good known fixed laid of things in addition to state, for illustration representing the land of Order every bit NEW, PARTIAL FILL, FILL or CLOSED.
Enumeration(Enum) was non originally available inward Java though it was available inward some other linguistic communication similar C in addition to C++, but eventually, Java realized in addition to introduced Enum on JDK v (Tiger) past times keyword Enum.
In this Java Enum tutorial, nosotros volition run into different Enum illustration inward Java in addition to acquire using Enum inward Java. Focus of this Java Enum tutorial volition last on different features provided past times Enum inward Java in addition to how to utilisation them.
If you lot receive got used Enumeration earlier inward C or C++ thence you lot volition non last uncomfortable with Java Enum but inward my opinion, Enum inward Java is to a greater extent than rich in addition to versatile than inward whatsoever other language.
By the way, if you lot similar to acquire novel concepts using majority thence you lot tin too run into Head First Java sec Edition, I had followed this majority spell learning Enum, when Java 1.5 was get-go launched. This majority has splendid chapter non alone on Enum but too on key features of Java 1.5 and worth reading.
How to stand upwards for enumerable value without Java enum
final constant to replicate enum similar behavior. Let’s run into an Enum illustration inward Java to empathize the concept better. In this example, nosotros volition utilisation U.S. of America Currency Coin every bit enumerable which has values similar PENNY (1) NICKLE (5), DIME (10), in addition to QUARTER (25).public class CurrencyDenom { public static final int PENNY = 1; public static final int NICKLE = 5; public static final int DIME = 10; public static final int QUARTER = 25; } public class Currency { private int currency; //CurrencyDenom.PENNY,CurrencyDenom.NICKLE, // CurrencyDenom.DIME,CurrencyDenom.QUARTER }
Though this tin serve our role it has some serious limitations:
1) No Type-Safety: First of all it’s non type-safe; you lot tin assign whatsoever valid int value to currency e.g. 99 though in that location is no money to stand upwards for that value.
2) No Meaningful Printing: printing value of whatsoever of these constant volition impress its numeric value instead of meaningful shout of money e.g. when you lot impress NICKLE it volition impress "5" instead of "NICKLE"
3) No namespace: to access the currencyDenom constant nosotros demand to prefix course of educational activity shout e.g. CurrencyDenom.PENNY instead of only using PENNY though this tin too last achieved past times using static import inward JDK 1.5
Java Enum is the reply of all this limitation. Enum inward Java is type-safe, provides meaningful String names in addition to has their ain namespace. Now let's run into the same illustration using Enum inward Java:
public enum Currency {PENNY, NICKLE, DIME, QUARTER};
Here Currency is our enum in addition to PENNY, NICKLE, DIME, QUARTER are enum constants. Notice curly braces only about enum constants because Enum is a type similar class and interface inward Java. Also, nosotros receive got followed the similar naming convention for enum similar course of educational activity in addition to interface (first missive of the alphabet inward Caps) in addition to since Enum constants are implicitly static final nosotros receive got used all caps to specify them similar Constants inward Java.
What is Enum inward Java
Enum constants are implicitly static in addition to final in addition to you lot tin non alter their value 1 time created. Enum inward Java provides type-safety in addition to tin last used within switch declaration similar int variables.
Since enum is a keyword you lot tin non utilisation every bit a variable shout in addition to since its alone introduced inward JDK 1.5 all your previous code which has an enum every bit a variable shout volition non operate in addition to needs to last refactored.
Benefits of using Enums inward Java
1) Enum is type-safe you lot tin non assign anything else other than predefined Enum constants to an Enum variable. It is a compiler fault to assign something else, dissimilar earth static in conclusion variables used inward Enum int pattern in addition to Enum String pattern.
2) Enum has its ain namespace.
3) The best characteristic of Enum is you tin utilisation Enum inward Java within Switch statement similar int or char primitive information type. We volition too run into an illustration of using coffee enum inward switch statement inward this coffee enum tutorial.
4) Adding novel constants on Enum inward Java is slow in addition to you lot tin add together novel constants without breaking the existing code.
Important points nearly Enum inward Java
1) Enums inward Java are type-safe in addition to has their ain namespace. It agency your enum volition receive got a type for illustration "Currency" inward below illustration in addition to you lot tin non assign whatsoever value other than specified inward Enum Constants.public enum Currency { PENNY, NICKLE, DIME, QUARTER }; Currency money = Currency.PENNY; money = 1; //compilation fault
2) Enum inward Java are reference types like class or interface and you lot tin define constructor, methods in addition to variables within coffee Enum which makes it to a greater extent than powerful than Enum inward C in addition to C++ every bit shown inward adjacent illustration of Java Enum type.
3) You tin specify values of enum constants at the creation time every bit shown inward below example:
public enum Currency {PENNY(1), NICKLE(5), DIME(10), QUARTER(25)};
But for this to operate you lot demand to define a fellow member variable in addition to a constructor because PENNY (1) is genuinely calling a constructor which accepts int value, run into below example.
public enum Currency { PENNY(1), NICKLE(5), DIME(10), QUARTER(25); private int value; private Currency(int value) { this.value = value; } };
The constructor of enum inward java must last private any other access modifier volition lawsuit inward compilation error. Now to acquire the value associated with each money you lot tin define a world getValue() method within Java enum similar whatsoever normal Java class. Also, the semicolon inward the get-go trouble is optional.
4) Enum constants are implicitly static and final and tin non last changed 1 time created. For example, below code of coffee enum volition lawsuit inward compilation error:
Currency.PENNY = Currency.DIME;
The in conclusion land EnumExamples.Currency.PENNY cannot last reassigned.
5) Enum inward coffee tin last used every bit an declaration on switch statement in addition to with "case:" similar int or char primitive type. This characteristic of coffee enum makes them rattling useful for switch operations. Let’s run into an illustration of how to utilisation coffee enum within switch statement:
Currency usCoin = Currency.DIME;
switch (usCoin) { case PENNY: System.out.println("Penny coin"); break; case NICKLE: System.out.println("Nickle coin"); break; case DIME: System.out.println("Dime coin"); break; case QUARTER: System.out.println("Quarter coin"); }
from JDK vii onwards you lot tin too String inward Switch instance inward Java code.
6) Since constants defined within Enum inward Java are in conclusion you lot tin safely compare them using "==", the equality operator every bit shown inward next illustration of Java Enum:
Currency usCoin = Currency.DIME; if(usCoin == Currency.DIME){ System.out.println("enum inward coffee tin last compared using =="); }
By the way comparison objects using == operator is non recommended, Always utilisation equals() method or compareTo() method to compare Objects.
If you lot are non convinced than you lot should read this article to acquire to a greater extent than nearly pros in addition to cons of comparison 2 enums using equals() vs == operator inward Java.
7) Java compiler automatically generates static values() method for every enum inward java. Values() method returns array of Enum constants inward the same lodge they receive got listed inward Enum in addition to you lot tin utilisation values() to iterate over values of Enum inward Java every bit shown inward below example:
for(Currency coin: Currency.values()){ System.out.println("coin: " + coin); }
And it volition print:
coin: PENNY coin: NICKLE coin: DIME coin: QUARTER
Notice the lodge is just the same as defined lodge inward the Enum.
8) In Java, Enum tin override methods also. Let’s run into an illustration of overriding toString() method inside Enum inward Java to provide a meaningful description for enums constants.
public enum Currency { ........ @Override public String toString() { switch (this) { case PENNY: System.out.println("Penny: " + value); break; case NICKLE: System.out.println("Nickle: " + value); break; case DIME: System.out.println("Dime: " + value); break; case QUARTER: System.out.println("Quarter: " + value); } return super.toString(); } };
And hither is how it looks similar when displayed:
Currency usCoin = Currency.DIME; System.out.println(usCoin); Output: Dime: 10
9) Two novel collection classes EnumMap and EnumSet are added into collection packet to support Java Enum. These classes are a high-performance implementation of Map in addition to Set interface inward Java and nosotros should utilisation this whenever in that location is whatsoever opportunity.
EnumSet doesn't receive got whatsoever world constructor instead it provides mill methods to create instance e.g. EnumSet.of() methods. This blueprint allows EnumSet to internally pick out betwixt 2 different implementations depending upon the size of Enum constants.
If Enum has less than 64 constants than EnumSet uses RegularEnumSet course of educational activity which internally uses a long variable to shop those 64 Enum constants in addition to if Enum has to a greater extent than keys than 64 thence it uses JumboEnumSet. See my article the difference betwixt RegularEnumSet in addition to JumboEnumSet for to a greater extent than details.
10) You tin non create an instance of enums past times using novel operator inward Java because the constructor of Enum inward Java tin alone last private in addition to Enums constants tin alone last created within Enums itself.
11) An instance of Enum inward Java is created when whatsoever Enum constants are get-go called or referenced inward code.
12) Enum inward Java tin implement the interface in addition to override whatsoever method similar normal course of educational activity It’s too worth noting that Enum inward coffee implicitly implements both Serializable and Comparable interface. Let's run into in addition to illustration of how to implement interface using Java Enum:
public enum Currency implements Runnable{ PENNY(1), NICKLE(5), DIME(10), QUARTER(25); private int value; ............ @Override public void run() { System.out.println("Enum inward Java implement interfaces"); } }
13) You tin define abstract methods within Enum inward Java in addition to tin too provide a different implementation for different instances of enum inward java. Let’s run into an example of using abstract method within enum inward java
public enum Currency { PENNY(1) { @Override public String color() { return "copper"; } }, NICKLE(5) { @Override public String color() { return "bronze"; } }, DIME(10) { @Override public String color() { return "silver"; } }, QUARTER(25) { @Override public String color() { return "silver"; } }; private int value; public abstract String color(); private Currency(int value) { this.value = value; }}
In this illustration since every money volition receive got the different color nosotros made the color() method abstract in addition to allow each instance of Enum to define their ain color. You tin acquire color of whatsoever money past times only calling the color() method every bit shown inward below illustration of Java Enum:
System.out.println("Color: " + Currency.DIME.color());
So that was the comprehensive listing of properties, behaviour in addition to capabilities of Enumeration type inward Java. I know, it's non slow to recollect all those powerful features in addition to that's why I receive got prepared this minor Microsoft powerpoint slide containing all of import properties of Enum inward Java. You tin ever come upwards dorsum in addition to depository fiscal establishment check this slide to revise of import features of Java Enum.
Real basis Examples of Enum inward Java
So far you lot receive got learned what Enum tin practise for you lot inward Java. You learned that enum tin last used to stand upwards for good known fixed laid of constants, enum tin implement interface, it tin last used inward switch instance similar int, curt in addition to String in addition to Enum has thence many useful built-in metods similar values(), vlaueOf(), name(), in addition to ordinal(), but nosotros didn't acquire where to utilisation the Enum inward Java?I think some existent basis examples of enum volition practise a lot of skillful to many pepole in addition to that's why I am going to summarize some of the pop usage of Enum inward Java basis below.
Enum every bit Thread Safe Singleton
One of the most pop utilisation of Java Enum is to impelment the Singleton blueprint pattern inward Java. In fact, Enum is the easieset way to create a thread-safe Singleton inward Java. It offering thence many payoff over traditional implementation using course of educational activity e.g. built-in Serialization, guarantee that Singleton volition ever last Singleton in addition to many more. I advise you lot to depository fiscal establishment check my article nearly Why Enum every bit Singelton is ameliorate inward Java to larn to a greater extent than on this topic.
Strategy Pattern using Enum
You tin too implement the Strategy blueprint pattern using Enumeration type inward Java. Since Enum tin implement interface, it's a skillful candidate to implement the Strategy interface in addition to define private strategy. By keeping all related Strategy inward 1 place, Enum offering ameliorate maintainence support. It too doesn't intermission the opened upwards shut blueprint regulation every bit per se because whatsoever fault volition last detected at compile time. See this tutorial to acquire how to implement Strategy pattern using Enum inward Java.
Enum every bit replacement of Enum String or int pattern
There is at 1 time no demand to utilisation String or integer constant to stand upwards for fixed laid of things e.g. condition of object similar ON in addition to OFF for a push or START, IN PROGRESS in addition to DONE for a Task. Enum is much ameliorate suited for those needs every bit it provide compile fourth dimension type security in addition to ameliorate debugging assistent than String or Integer.
Enum every bit State Machine
You tin too utilisation Enum to impelment State machine inward Java. Influenza A virus subtype H5N1 State machine transition to predifine laid of states based upon electrical current land in addition to given input. Since Enum tin implement interface in addition to override method, you lot tin utilisation it every bit State machine inward Java. See this tutorial from Peter Lawrey for a working example.
Enum Java valueOf example
One of my readers pointed out that I receive got non mentioned nearly the valueOf method of enum inward Java, which is used to convert String to enum inward Java.
Here is what he has suggested, thank you lot @ Anonymous
“You could too include valueOf() method of enum inward coffee which is added past times compiler inward whatsoever enum along with values() method. Enum valueOf() is a static method which takes a string declaration in addition to tin last used to convert a String into an enum. One think though you lot would similar to continue inward heed is that valueOf(String) method of enum volition throw "Exception inward thread "main" java.lang.IllegalArgumentException: No enum const class" if you lot render whatsoever string other than enum values.
Another of my reader suggested nearly ordinal() in addition to name() utility method of Java enum Ordinal method of Java Enum returns the seat of a Enum constant every bit they declared inward enum spell name()of Enum returns the exact string which is used to create that exceptional Enum constant.” name() method tin too last used for converting Enum to String inward Java.
That’s all on Java enum, Please part if you lot receive got whatsoever overnice tips on enum inward Java in addition to allow us know how you lot are using coffee enum inward your work. You tin too follow some skillful advice for using Enum past times Joshua Bloch inward his all fourth dimension classic majority Effective Java. That advice volition give you lot to a greater extent than thought of using this powerful characteristic of Java programming language
Further Reading on Java Enum
If you lot similar to acquire to a greater extent than nearly this cool feature, I advise reading next books. Books are 1 of the best resources to completely empathize whatsoever topic in addition to I personally follow them every bit well. Enumeration types chapter from Thinking inward Java is especially useful.
- Thinking inward Java (4th Edition) By Bruce Eckel
- Effective Java past times Joshua Bloch
- Java 5.0 Tiger: Influenza A virus subtype H5N1 Developers notebook
- Java vii Recipes
The in conclusion majority is suggested past times 1 of our reader @Anonymous, you lot tin run into his comment
Check out the book, Java vii Recipes. Chapter four contains some skillful content on Java enums. They genuinely expire into depth in addition to the examples are excellent.
Some Java Tutorials you lot May Like
Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!
0 Response to "Java Enum Tutorial: Ten Examples Of Enum Inward Java"
Post a Comment