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 a fixed number of well-known things like the 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 a 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.



1. What is Enum in Java?

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


2. What is the use of Enum in Java?

As I told in the previous example, you can use Enum to represent a 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 a Strategy pattern. You can use Enum to write thread-safe Singleton, and even to create your own state machine in Java. 


3. 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.



4. 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.


5. Can Enum have a 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.



7. 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 that has a method getHexCode(). Now, you declare four colors or enum constants in Color enum e.g. RED, BLUE, GREEN, WHITE, BLACK and 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.




8. 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 a compile-time error.


9. Can Enum implement an interface and override method in Java?

Yes, Enum in Java can implement an 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. 


10. How to compare two Enumeration in Java?

Enum instance in Java can be compared using == equality operator or equals() method in Java. Enum provides 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.


11. How to compare the 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


12. 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.



13. 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.


14. How to iterate over Enum in Java?

You can use Enum's values() method that can be used to iterate over an 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 the 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. 


Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!

Related Java Enum Tutorials for further reading :
  1. Top 15 Java Enum Interview Questions with Answers (see here)
  2. Difference between RegularEnumSet and JumboEnumSet in Java (read here)
  3. Learn how to use Enum in Java with a Simple Example (check here)
  4. Java tip to convert  Enum to String in Java (see tip)
  5. How to use valueOf method of Enum (check here)
  6. String to Enum in Java with Example (check here)
  7. Can we use Enum in Switch Statement in Java (Yes)
  8. How to loop over Enum constants in Java (example)
  9. What Every Java Programmer Should know about Enum (click here)
  10. 10 points about Enum in Java (see here)
  11. Can Enum have Constructor in Java (learn here)
Thanks for reading this article so far. If you find this article useful then please share in Facebook, Twitter, and LinkedIn. 

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