Skip to content

Enums in Java

Enums are special lists of words we use in our programs. These words are like superstars—they never change and always stay the same. Whether it's days of the week, colors, or directions, enum classes help us keep things simple and tidy.

Let's check out a quick example:

java
public enum DayOfWeek {
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY
}

public class EnumExample {
    public static void main(String[] args) {
        DayOfWeek today = DayOfWeek.TUESDAY;
        System.out.println("Today is: " + today);
    }
}

Pretty neat, huh? Our enum class DayOfWeek lists all the days of the week. When we run our program, we can easily tell which day it is by printing out the value of today.

But wait, there's more fun to be had with enum classes! We can also give each word in our list special powers, like little superheroes. Let's see an example:

java
public enum Direction {
    NORTH {
        @Override
        public String getSymbol() {
            return "^";
        }
    },
    SOUTH {
        @Override
        public String getSymbol() {
            return "v";
        }
    },
    EAST {
        @Override
        public String getSymbol() {
            return ">";
        }
    },
    WEST {
        @Override
        public String getSymbol() {
            return "<";
        }
    };

    public abstract String getSymbol();
}

public class EnumExample {
    public static void main(String[] args) {
        Direction facing = Direction.NORTH;
        System.out.println("You're facing: " + facing.getSymbol());
    }
}

Now that's super cool! Our Direction enum not only lists the directions but also gives each direction its own special symbol. When we print out the symbol for the direction we're facing, it's like having a little map right in our code!

Enum Constrctors

Enums as a special type in Java that allows you to define a set of named constants. Enum constructors are simply special methods used to initialize these constants, providing them with unique behaviors or properties.

Example:

java
public enum Day {
    MONDAY("Start of the week"),
    TUESDAY("Second day"),
    WEDNESDAY("Midweek"),
    THURSDAY("Almost there"),
    FRIDAY("End of the week"),
    SATURDAY("Weekend"),
    SUNDAY("Relaxation day");

    private final String description;

    Day(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }
}

public class EnumConstructorExample {
    public static void main(String[] args) {
        for (Day day : Day.values()) {
            System.out.println(day + ": " + day.getDescription());
        }
    }
}

In our example, we have an enum called Day, where each constant represents a day of the week. But here's where the magic happens—the enum constructor Day(String description) allows us to initialize each constant with a descriptive string. It's like giving each day its own unique personality!

Waytojava is designed to make learning easier. We simplify examples for better understanding. We regularly check tutorials, references, and examples to correct errors, but it's important to remember that humans can make mistakes.