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 Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Creating Your Swiss Army Knife on Java Test Stack
  • I Don’t TDD: Pragmatic Testing With Java
  • Spring 5 Web Reactive: Flux, Mono, and JUnit Testing
  • Why Testing is a Long-Term Investment for Software Engineers

Trending

  • Performing and Managing Incremental Backups Using pg_basebackup in PostgreSQL 17
  • Unmasking Entity-Based Data Masking: Best Practices 2025
  • AI-Based Threat Detection in Cloud Security
  • How Trustworthy Is Big Data?
  1. DZone
  2. Coding
  3. Java
  4. Using Java Enums

Using Java Enums

See Java enums in action and learn their best practices

By 
John Thompson user avatar
John Thompson
·
Apr. 27, 18 · Tutorial
Likes (37)
Comment
Save
Tweet
Share
385.1K Views

Join the DZone community and get the full member experience.

Join For Free

In applications, you often need to work with a set of constant values. For example, representing a contract status with the “permanent”, “temp”, and “intern” values, or directions with the “north”, “south”, “east”, and “west” values.

In Java, you use the enum type (short for enumeration), a special datatype introduced in Java 5 to represent such lists of predefined constants.

In this article, I’ll discuss how to define and use enum types. I’ll also include example code along with JUnit test cases. If you are new to JUnit, I suggest going through my JUnit series of posts.

Defining Java Enums

You can define an enum type either independently outside of any class or as part of a class. The code to define an enum independently is:

package springframework.guru.enumexample;
enum Days{
   SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
}


The preceding code defines an enum type, Days. The names of the enum type’s fields are in uppercase letters, as they are constants.

Here is the JUnit test code.

package springframework.guru.enumexample;

import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class SimpleEnumExampleTest {
   @Test
   public void simpleEnumExampleOutsideClassTest(){
       Days day = Days.SUNDAY;
       System.out.println("Days enum is set a value: "+day);
       assertEquals(Days.valueOf("SUNDAY"), day);
   }
}


The test code assigns a value to the day variable of the enum type, Days. The day variable can take any of the seven defined values. In this case, it is set to SUNDAY. The test code then asserts the value.

The output of running the test in IntelliJ is: