DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > 18 Points Every Java Developer Should Know About Enums In Java

18 Points Every Java Developer Should Know About Enums In Java

Want to learn more about using enums in Java? Check out this post on the top 18 points that every Java developer should know about enums.

Pramod Bablad user avatar by
Pramod Bablad
·
Aug. 18, 18 · Java Zone · Presentation
Like (31)
Save
Tweet
30.92K Views

Join the DZone community and get the full member experience.

Join For Free

In the following post, we will take a look at the top 18 tips for working with Java enums. Let's get into it!

  1. Even though it is not mandatory to declare enum constants with UPPERCASE letters, it is in the best practice to do so.

  2. Enum types, like classes, can have fields, constructors, and methods along with enum constants.

  3. Enum constructors are private by default. Only private constructors are allowed in enum types. That’s why you can’t instantiate enum types using a new operator.

  4. Enum constants are created only once for the whole execution. All enum constants are created when you initially refer any enum constant in your code. While creating each enum constant, the corresponding constructor is called.

  5. Enum constants must be declared before fields, constructors, and methods (if any).

  6. All enum types extend the java.lang.Enum class by default. As multiple inheritances are not supported in Java, they can’t extend to any other classes.

  7. Enum types can implement any number of interfaces.

  8. Enum constants can have their own body called Constant Specific Body. In that body, you can define fields and methods. But, these methods and fields are visible within the Constant Specific Body in which they were defined.

  9. Enum types are final by default. They cannot be extended by any other types.

  10. For every enum type written in a file, the .class file will be generated after compilation.

  11. Enum types can have any number of static initialization blocks as well as instance initialization blocks.

  12. Since the java.lang.Enum class implements theComparable  and Serializable  interface, all enum types are Comparable and Serializable by default.

  13. We can compare the enum constants using the  == operator.

  14. You can retrieve the enum constants of any enum type using the values() method. The values() method returns an array of enum constants.

  15. Enums provide type-safety during compilation. That means you will get a compile-time error if you try to assign any values other than the specified enum constants.

  16. You can define enum types outside a class or inside a class but not inside a method or block.

  17. The  ordinal() method is used to get the order of an enum constant in an enum type.

  18. Enums are mostly used when you want to allow a limited set of options that remain constant for the whole execution and you know all possible options at compile time. For example, this could be choices on a menu or options of a combobox.

See more at https://javaconceptoftheday.com/18-points-every-java-developer-should-know-about-enums-in-java/

Further Reading on Java Enums 

  • https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

  • https://javaconceptoftheday.com/java-enums-tutorial-with-examples/

  • https://javaconceptoftheday.com/java-practice-coding-questions-on-enum-types/

Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Deploy Apache Kafka With Kubernetes
  • A First Look at CSS When and Else Statements
  • Kafka Fail-Over Using Quarkus Reactive Messaging
  • Testing Under the Hood Or Behind the Wheel

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo