I suggest you check my article about Why Enum as Singelton is better in Java to learn more on this topic. built-in Serialization, guarantee that Singleton will always be Singleton, and many more. It offers so many advantages over traditional implementation using class e.g. In fact, Enum is the easiest way to create a thread-safe Singleton in Java. One of the most popular uses of Java Enum is to implement the Singleton design pattern in Java. I think some real-world examples of enum will do a lot of good to many people and that's why I am going to summarize some of the popular usages of Enum in the Java world below. You learned that enum can be used to represent well known fixed set of constants, enum can implement an interface, it can be used in switch cases like int, short and String and Enum has so many useful built-in methods like values(), the valueOf (), name(), and ordinal(), but we didn't learn where to use the Enum in Java? Real-world Examples of Enum in Java So far you have learned what Enum can do for you in Java. It means your enum will have a type for example "Currency" in the below example and you can not assign any value other than specified in Enum Constants. Important points about Enum in Java 1) Enums in Java are type-safe and have their own namespace. We will also see an example of using java enum in the switch statement in this java enum tutorial.Ĥ) Adding new constants on Enum in Java is easy and you can add new constants without breaking the existing code. It is a compiler error to assign something else, unlike the public static final variables used in the Enum int pattern and Enum String pattern.ģ) The best feature of Enum is you can use Enum in Java inside Switch statements like int or char primitive data type. Since enum is a keyword you can not use as a variable name and since it's only introduced in JDK 1.5 all your previous code which has an enum as a variable name will not work and needs to be refactored.ġ) Enum is type-safe you can not assign anything else other than predefined Enum constants to an Enum variable. Enum in Java provides type-safety and can be used inside switch statements like int variables. Now back to primary questions “What is Enum in java” simple answer Enum is a keyword in java and in more detail term Java Enum is a type like class and interface and can be used to define a set of Enum constants.Įnum constants are implicitly static and final and you can not change their value once created. Now let's see the same example using Enum in Java: Enum in Java is type-safe, provides meaningful String names and has their own namespace. Java Enum is the answer of all these limitations. CurrencyDenom.PENNY instead of just using PENNY though this can also be achieved by using static import in JDK 1.5 when you print NICKLE it will print "5" instead of "NICKLE"ģ) No namespace: to access the currencyDenom constant we need to prefix class name e.g. 99 though there is no coin to represent that value.Ģ) No Meaningful Printing : printing value of any of these constants will print its numeric value instead of the meaningful name of coin e.g. Though this can serve our purpose it has some serious limitations:ġ) No Type-Safety : First of all it’s not type-safe you can assign any valid int value to currency e.g. Private int currency //CurrencyDenom.PENNY,CurrencyDenom.NICKLE, // CurrencyDenom.DIME,CurrencyDenom.QUARTER I use a public static final constant to replicate enum like behavior. How to represent enumerable value without Java enum Since Enum in Java is only available from Java 1.5 it's worth discussing how we used to represent enumerable values in Java prior to JDK 1.5 and without it. This book has an excellent chapter not only on Enum but also on key features of Java 1.5 and is worth reading. If you have used Enumeration before in C or C++ then you will not be uncomfortable with Java Enum but in my opinion, Enum in Java is more rich and versatile than in any other language.īy the way, if you like to learn new concepts using a book then you can also see Head First Java 2nd Edition, I had followed this book while learning Enum when Java 1.5 was first launched. The focus of this Java Enum tutorial will be on different features provided by Enum in Java and how to use them. In this Java Enum tutorial, we will see different Enum example in Java and learn using Enum in Java. By the way, Java Enum as a type is more suitable to represent well known fixed set of things and states, for example representing the state of Order as NEW, PARTIAL FILL, FILL, or CLOSED.Įnumeration(Enum) was not originally available in Java though it was available in another language like C and C++, eventually, Java realized and introduced Enum on JDK 5 (Tiger) by keyword Enum.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |