Loading...

Java Enums

List a fixed set of names with enum so the program picks from that list only. Write enum Level with LOW, MEDIUM, and HIGH. In Yard.java, assign Level mark = Level.MEDIUM; and print mark. Compile with javac Yard.java and run java Yard. The screen should show MEDIUM. A second demo runs a switch on the same mark and prints medium. Leave abstract classes for the next page — this lesson is a short list of fixed names and how you print or branch on one of them.

Print one enum name

The interfaces page filled empty method promises with implements. This page lists constants instead. Open Yard.java. Keep the public class name Yard. Delete leftover Signal / implements demos if they bury the enum story, leftover Truck horn files, leftover path demos, and any keyboard Scanner. The program must not wait for typing. No import line is required.
Declare the enum above the public class:
enum Level {
LOW,
MEDIUM,
HIGH
}
Separate the names with commas. Keep them uppercase on this beginner shape. The last name needs no trailing comma for this teaching file.
Inside Yard.main, pick one name and print it:
Level mark = Level.MEDIUM;
System.out.println(mark);
You do not write new Level(). You write the type name, a dot, and the constant: Level.MEDIUM.

Yard print Level.MEDIUM

java
enum Level {
    LOW,
    MEDIUM,
    HIGH
}

public class Yard {
    public static void main(String[] args) {
        Level mark = Level.MEDIUM;
        System.out.println(mark);
    }
}

From the folder that holds Yard.java, run javac Yard.java. When parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Yard — not java Yard.java. Leave the .java ending off the run command. Keep one public class in the file so the name matches Yard. The enum Level stays without public on this beginner shape when it sits beside Yard in the same file.
The screen shows MEDIUM. Walk the run once. Java starts at Yard.main. The variable mark holds the constant Level.MEDIUM. Printing mark writes that constant’s name. The other names LOW and HIGH stay available on the type, but this demo only prints the one you assigned.
Read the assignment line once more: the type is Level, the value is Level.MEDIUM. Both sides belong to the same enum. You are not building a free text string; you are picking a listed name. Keep that habit when you later change the mark to LOW or HIGH in a private experiment — only names from the enum list are legal.
If you still see honk from an interface file, switch to the enum teaching file and run java Yard again. If javac complains about a missing constant, check the spelling and the uppercase form of MEDIUM. If you try to invent a fourth name at the call site without listing it in the enum, javac refuses — the list in the enum is the allowed set.
Say the rule out loud while you watch the screen: enum lists the names, code picks one with a dot, print shows that name. Do not invent an abstract class on this page. Abstract classes wait for the next lesson.

Branch on the enum with switch

A second program keeps the same Level list. main still assigns Level.MEDIUM. A switch on mark prints a lowercase word for the chosen case.

Yard switch on Level.MEDIUM

java
enum Level {
    LOW,
    MEDIUM,
    HIGH
}

public class Yard {
    public static void main(String[] args) {
        Level mark = Level.MEDIUM;
        switch (mark) {
            case LOW:
                System.out.println("low");
                break;
            case MEDIUM:
                System.out.println("medium");
                break;
            case HIGH:
                System.out.println("high");
                break;
        }
    }
}

Compile with javac Yard.java, then run java Yard. The screen shows medium. The switch entered the MEDIUM case because mark held Level.MEDIUM. The break stops that case so the later cases do not run. Change only the assigned constant in a later experiment if you want to see low or high — keep the enum list the same.
Inside each case, write the bare constant name: case MEDIUM:, not case Level.MEDIUM:. The switch already knows the type from switch (mark). Keep a break after each print on this beginner shape so the teaching output stays one clear line. Walk the three cases with your eyes once: LOW maps to low, MEDIUM maps to medium, HIGH maps to high. Only one of those prints runs for a single assignment.
If leftover interface demos still sit nearby, keep them as history. For this lesson run the enum teaching file so the screen shows MEDIUM or medium, not honk. If javac complains about duplicate public classes, you still have more than one public type in the file — keep only Yard public.

The three Level names

📊 Level enum names

Name Role on this page
LOW listed constant; switch can print low
MEDIUM assigned and printed in both demos
HIGH listed constant; switch can print high
The table is the short map of this page. The enum holds all three names. The first demo prints the middle name as MEDIUM. The second demo maps that same choice to the lowercase word medium. Keep the list short and fixed; do not treat the mark as a free typed String on this page.

Compared with interfaces

On the interfaces page, Signal promised empty methods and Truck implements Signal filled them. On this page, enum Level lists names and Yard picks one. You still compile Yard.java and run java Yard. The new tokens are enum, the uppercase constant list, Level.MEDIUM, and — in the second demo — switch cases on those names.
If a leftover implements Signal file is still open, you will see method bodies and honk. Switch to the enum file so the top shows enum Level and main assigns a constant. Keep abstract-class demos off this page. Keep keyboard input off this page.

Before abstract classes

You can declare enum Level with LOW, MEDIUM, and HIGH, assign Level.MEDIUM, and see MEDIUM on the screen. You can switch on that mark and see medium. Compile Yard.java and run java Yard for each kept demo in turn. The program picked from a fixed list. The next page is abstract classes, where a parent type can leave a method without a full body for children to finish.
Keep the print demo for a clear constant on the screen. Keep the switch demo for a clear branch on that constant. Keep abstract files off this page. Those three habits are the checkpoint before you move on. When MEDIUM appears from a direct print — and when medium appears from the switch — enums did their job for this lesson and you are ready for the next topic about abstract classes.
One last pass through the folder: one public Yard, one enum Level with three names, no keyboard wait, no forced abstract class. Run javac Yard.java and java Yard once more on the demo you keep open. If the screen still matches the table’s middle row story, the enum list is solid and the course can move to abstract classes on the next page.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
5
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top