DZone
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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Java
  4. 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 · Presentation
Like (31)
Save
Tweet
Share
31.27K 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

  • Detecting Network Anomalies Using Apache Spark
  • Asynchronous Messaging Service
  • The Power of Docker Images: A Comprehensive Guide to Building From Scratch
  • How Chat GPT-3 Changed the Life of Young DevOps Engineers

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: