Skip to content
QuizMaker logoQuizMaker
Activity
Java Programming: From Zero to Enterprise
2. Object-Oriented Programming
1. Getting Started with Java & the JVM
2. Data Types & Variables
3. Control Flow: Ifs & Loops
4. String Manipulation in Depth
5. Methods (Functions) Architecture
6. Arrays & The Enhanced For Loop
7. User Input via Scanner
8. Mathematical Operations & The Math Class
9. Operators in Depth
10. Block Scope & Variable Lifecycles
11. Introduction to Object-Oriented Programming
12. Classes & Instances Deep Dive
13. Constructors
14. Encapsulation & The 'this' Keyword
15. Inheritance: Extending Functionality
16. Polymorphism & Method Overriding
17. Abstraction & Abstract Classes
18. Interfaces: The Ultimate Contract
19. Packages & Access Modifiers
20. Enums (Enumerations)
21. Exceptions: Handling Runtime Errors
22. The 'throw' and 'throws' keywords
23. Dates, Times, and Formatting
24. Enumerable Data Structures
25. LinkedLists: The Alternative
26. HashMaps: Key-Value Architecture
27. HashSets: The Art of Uniqueness
28. Iterator: Safe Collection Traversal
29. Wrapper Classes & Autoboxing
30. Basic File I/O
31. Generics: Type-Safe Templates
32. Lambda Expressions & Functional Interfaces
33. The Stream API: Functional Data Pipelines
34. Optional: Beating the NullPointerException
35. Multithreading & Concurrency Basics
36. JDBC: Connecting to SQL Databases
37. Annotations & Reflection
38. The JVM Garbage Collector
39. Introduction to Spring Boot
40. Unit Testing with JUnit
41. Java Collections for DSA
CONTENTS

20. Enums (Enumerations)

Locking down variables to a specifically constrained list of options.

Feb 22, 20264 views0 likes0 fires
18px

[!NOTE] Often, an application has a variable that should only ever be one of three or four specific things. A user's role might be Admin, Moderator, or Guest. If you represent this as a String, a developer might accidentally set a user's role to "Potato".

We solve this using Enums.

Defining an Enum

An enum is a special "class" that represents a group of constants (unchangeable variables).

// We declare all possible valid states globally
public enum Level {
  LOW,
  MEDIUM,
  HIGH
}

Now, when you build a class, you don't use a String for the difficulty variable. You strictly type it to the Level enum!

public class GameSession {
    // The type is 'Level', not 'String'!
    Level currentDifficulty;

    public GameSession() {
        // You cannot assign "SUPER_HARD". The compiler restricts you to the 3 constants.
        currentDifficulty = Level.MEDIUM; 
    }
}

Using Enums in Switch Statements

Enums shine the brightest when combined with a switch statement. It prevents developers from having to memorize random integer codes (like "Role 1 is Admin, Role 2 is Guest").

Level myVar = Level.HIGH;

switch(myVar) {
  case LOW:
    System.out.println("Low level");
    break;
  case MEDIUM:
     System.out.println("Medium level");
    break;
  case HIGH:
    System.out.println("High level executed!");
    break;
}

Advanced Enums

Because Enums in Java are effectively fully-featured classes behind the scenes, you can actually add instance variables, constructors, and methods directly inside the Enum definition itself!

public enum Role {
    // Define the enums and pass a parameter into their secret constructor!
    ADMIN(99),
    MODERATOR(50),
    GUEST(1);

    private int powerLevel;

    // Private constructor used ONLY internally by the Enum creation above
    private Role(int power) {
        this.powerLevel = power;
    }

    public int getPowerLevel() {
        return this.powerLevel;
    }
}

// Somewhere else in code:
System.out.println(Role.ADMIN.getPowerLevel()); // Outputs 99!

Share this article

Share on TwitterShare on LinkedInShare on FacebookShare on WhatsAppShare on Email

Test your knowledge

Take a quick quiz based on this chapter.

easyJava
Quiz: Enums
11 questions5 min

Continue Learning

21. Exceptions: Handling Runtime Errors

Intermediate
12 min

22. The 'throw' and 'throws' keywords

Intermediate
10 min

23. Dates, Times, and Formatting

Beginner
8 min
Lesson 10 of 10 in 2. Object-Oriented Programming
Previous in 2. Object-Oriented Programming
19. Packages & Access Modifiers
Completed
You finished this lesson → take the quiz
11 questions • 5 min
Next section: 3. Core APIs & I/O
← Back to Java Programming: From Zero to Enterprise
Back to Java Programming: From Zero to EnterpriseAll Categories