Java enum
A enum in Java is a special data type used to define a collection of named constants—unchangeable, type-safe values. Enums are used when all possible values are known at compile time, such as days of the week, directions, or states in a process. They make code more readable, safer, and less error-prone.
Java Enum Syntax
enum EnumName {
CONSTANT1,
CONSTANT2,
CONSTANT3
}
Parameters:
EnumName: The name of the enum (should follow Java naming conventions)CONSTANT1, CONSTANT2, CONSTANT3: Enum constants (should be in uppercase)
Return value:
Java enum constants are objects of the enum type and can be accessed using the dot notation.
Example 1: Java Enum Declaration
The following example demonstrates how to declare enum in Java, both outside and inside a class:
// Java enum declared outside any classenum Priority {LOW,MEDIUM,HIGH}public class TaskManager {// Java enum declared inside a classenum Status {PENDING,IN_PROGRESS,COMPLETED}public static void main(String[] args) {// Using java enum constantsPriority taskPriority = Priority.HIGH;Status taskStatus = Status.PENDING;System.out.println("Task Priority: " + taskPriority);System.out.println("Task Status: " + taskStatus);}}
This example results in the following output:
Task Priority: HIGHTask Status: PENDING
The java enum constants Priority.HIGH and Status.PENDING are accessed using dot notation and can be assigned to variables of their respective java enum type.
Example 2: Looping Through Java Enum Constants
Enum in java provides a built-in values() method that returns an array of all enum constants, making it easy to iterate through them:
enum Color {RED,GREEN,BLUE,YELLOW}public class ColorPrinter {public static void main(String[] args) {System.out.println("Available colors:");// Loop through all java enum constantsfor (Color color : Color.values()) {System.out.println("- " + color);}}}
This example results in the following output:
Available colors:- RED- GREEN- BLUE- YELLOW
The values() method returns all java enum constants as an array, which can be iterated using an enhanced for loop.
Example 3: Using Java Enums in a Switch Statement
Java enum works seamlessly with switch statements, providing a clean way to handle different cases based on enum in java values:
enum OrderStatus {PLACED,PROCESSING,SHIPPED,DELIVERED,CANCELLED}public class OrderManager {public static void processOrder(OrderStatus status) {switch (status) {case PLACED:System.out.println("Order has been placed. Processing soon.");break;case PROCESSING:System.out.println("Order is being processed.");break;case SHIPPED:System.out.println("Order has been shipped.");break;case DELIVERED:System.out.println("Order delivered successfully!");break;case CANCELLED:System.out.println("Order has been cancelled.");break;default:System.out.println("Unknown order status.");break;}}public static void main(String[] args) {OrderStatus currentStatus = OrderStatus.SHIPPED;processOrder(currentStatus);}}
This example results in the following output:
Order has been shipped.
Switch statements with java enum are type-safe and don’t require the enum class name prefix within case labels.
Example 4: Java enum with Constructor and Methods
Enum in java can have constructors, fields, and methods, making them powerful tools for organizing related data and behavior:
enum Size {SMALL("S", 32),MEDIUM("M", 36),LARGE("L", 40),EXTRA_LARGE("XL", 44);private final String code; // Size codeprivate final int chestSize; // Chest measurement in inches// Java enum constructorSize(String code, int chestSize) {this.code = code;this.chestSize = chestSize;}// Method to get size codepublic String getCode() {return code;}// Method to get chest measurementpublic int getChestSize() {return chestSize;}// Method to check if size fits given measurementpublic boolean fits(int measurement) {return measurement <= chestSize;}}public class ClothingStore {public static void main(String[] args) {int customerChest = 38;System.out.println("Available sizes:");for (Size size : Size.values()) {System.out.println(size + " (" + size.getCode() + "): " + size.getChestSize() + " inches");}System.out.println("\nCustomer chest size: " + customerChest + " inches");System.out.println("Recommended sizes:");for (Size size : Size.values()) {if (size.fits(customerChest)) {System.out.println("- " + size + " fits!");}}}}
This example results in the following output:
Available sizes:SMALL (S): 32 inchesMEDIUM (M): 36 inchesLARGE (L): 40 inchesEXTRA_LARGE (XL): 44 inchesCustomer chest size: 38 inchesRecommended sizes:- LARGE fits!- EXTRA_LARGE fits!
The java enum constructor is called automatically for each constant, allowing enums to store multiple pieces of data (size code and chest measurement) and provide methods like fits() that operate on that data to solve real-world problems.
Frequently Asked Questions
1. Why is java enum required in Java?
Java enum provides type safety and prevents invalid values from being assigned. They make code more readable by using meaningful names instead of numeric constants, reduce bugs by eliminating magic numbers, and enable compile-time checking of valid values.
2. How to name java enum in Java?
Java enum names should follow Java class naming conventions (PascalCase), while enum in java constants should be in uppercase with underscores separating words (UPPER_SNAKE_CASE). For example: enum HttpStatus { OK, NOT_FOUND, INTERNAL_SERVER_ERROR }.
3. What is the difference between java enum and constant in Java?
Java enum are type-safe and provide additional functionality like methods and constructors, while constants are simply final variables. Enum in java prevents invalid assignments and provides built-in methods like values() and valueOf(), whereas constants offer no such protection or functionality.
All contributors
1P1E10- Anonymous contributor
Victoria-DR
christian.dinh- Anonymous contributor
- MamtaWardhani
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
- Beginner Friendly.17 hours