Java Enum FAQ - What Every Java Developer should know about Enumeration type

Java Enum was one of the best features added on JDK 1.5 Tiger release along with Generics, Autoboxing, and varargs. Enum in Java represents fixed number of well-known things e.g. number of days in Week, the number of months in the calendar year etc. Unlike C and C++ Enum in Java are much powerful and they are not an integer constant, Instead, Enum is a Type like class or interface which provides compile time type safety. In this Java Enum tutorial, we will see a couple of important points about Java Enum which help to understand Enum better and get most out of it. I have written in the style of FAQ i.e. frequently asked questions, this will help you to learn more about Java Enum's capability and features e.g. whether Enum can extend class or implement methods or not? When you can override methods inside enum? whether you can compare Enum using equals() or == in Java? etc


Java Enum FAQ - Frequently asked Questions

As I told you enum or enumeration type in Java is one of the important concepts in Java. It's not just enough to learn about class and interface, you also have to learn about Enumeration type. In this article, I will answer some of the frequently asked questions based upon Java Enum, these will help you to learn the concept better and motivate you to explore by yourself.



What is Enum in Java?
Enum is Java construct much like a class and interface which is used to represent fixed number of well-known things e.g. days of the week, months of a year etc.


What is the use of Enum in Java?
As I told in the previous example, you can use Enum to represent fixed number of things e.g. you can use it represent the states of your application e.g. threads states like Runnable, Running, Stopped etc. You can also use it to implement Strategy pattern. You can use Enum to write thread-safe Singleton, and even to create your own state machine in Java. 


What is the difference between C, C++ enum and Java Enum?
Unlike C and C++, Enum in Java are not integer constant, they are classes, similar to any interface or class. Because of this reason Enum in Java provides type safety and compile time checking which was not possible with enum int pattern. Java Enum is a full-fledged type and you can use it as class or interface.



Can Enum extend a Class in Java?
No, Enum cannot extend a Class in Java. Every Enum in Java extends from java.lang.Enum implicitly and because of this Enum can not extend any other class in Java.


Can Enum have Constructor in Java?
Enum in Java doesn't have a public constructor. This means you can not create an additional instance of Enum instances other than what is created from Enum class itself and available at compile time. Enum can have a private constructor, though.



Can you declare methods inside Enum in Java?
Yes, you can declare methods in Java Enum and you can even override and overload them inside individual Enum instances. For example, you have declared a Color enum which has a method getHexCode(). Now, you declare four colors or enum constants in Color enum e.g. RED, BLUE, GREEN, WHITE, BLACKand then you can override getHexCode() method to return hex code for these color e.g. RED will return 0xFF0000 and BLACK will return 0x000000.

You can further read Core Java, Volume 1 - Fundamentals by Cay S. Horstmann to learn more about advanced features of Enum in Java.




Can you change the value of Enum instance in Java?
Enum values or Enum instances are public, static and final in Java. They are a compile-time constant, which means you can not changes values of Enum instances and further assignment will result in compile time error.


Can Enum implement an interface and override method in Java?
Yes, Enum in Java can implement interface and override methods. In fact, Java Enum implicitly implements Comparable interface and override compareTo() method. You can check the code of java.lang.Enum class to confirm this. 


How to compare two Enumeration in Java?
Enum instance in Java can be compared using == equality operator or equals() method in Java. Enum provides the high-quality implement for equals() and hashCode and can be used in Set and Map. But for performance reason, it's best to use EnumMap and EnumSet while storing enum in Collection.


How to compare same instances of the same Enum in Java?
When you compare different instances of the same Enum type, they are compared in the order on which they are declared in Enum. You can get this order by calling the ordinal() method of Enum.

Java Enum FAQ - What Every Java Developer should know about Enumeration type


How to convert Enum to String in Java?
Enum provides implicit methods like values(), ordinal() and name() which can be used to get all Enum instances as an array, order of enum as declared in class and String name of Enum instances in Java.



How to convert Enum to Array in Java?
You can use Enum's values() to convert an Enum to array in Java. This method returns an array containing all Enum constants. See these examples to learn more about how to use values() method of java.lang.Enum in Java.


How to iterate over Enum in Java?
You can use Enum's values() method can be used to iterate over enum. It returns an array which can be traversed using any loop constructs in Java, including Java 1.5 enhanced for loop.


That's all about some frequently asked questions about Enumeration type in Java. Enum is much powerful and they have used in different ways in Java programming language. you can even use Enum to create thread-safe Singleton in Java. You can also replace your old code which is written using enum int pattern or enum String pattern in Java.

If you have any questions related to Enum in Java then please drop a comment and I'll try to answer your questions. If you want to learn more you can further read Head First Java, it explains Enum concepts quite well. 


2 comments:

  1. these tutorials are very usefull to me....... thank u

    ReplyDelete
  2. >>>>>>>>>>
    4) Enum values or Enum instances are public, static and final in Java. They are compile time constant,
    which means you can not changes values of Enum instances and further assignment will result in
    compile time error.
    >>>>>>>>


    This is mostly true. You can, however, define a set-method and change internal values of enums at runtime! This is a no-go but technically it is possible.

    ReplyDelete