codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

37 / 65 topics
32Java Inheritance33Java Polymorphism34Java Inner Classes35Java Abstraction36Java Interface37Java Enum
Tutorials/Java Programming/Java Enum
☕Java Programming

Java Enum

Updated 2026-05-12
30 min read

Java Enum

In this tutorial, we will explore the versatile concept of enums (short for enumerations) in Java. Enums are a special data type that enables a variable to be a set of predefined constants. They are particularly useful when you have a fixed set of values that a variable can take. Understanding enums is crucial as they enhance code readability and maintainability. This tutorial will cover the basics of enums, how to use them in switch statements, and how to loop through enum values.

Introduction

Enums allow you to define a collection of constants that are logically related. For instance, days of the week, months, or states can be represented as enums. Using enums makes your code more readable and less error-prone by restricting variables to only predefined values.

In this section, we will delve into how enums can be used in inheritance and polymorphism, which are fundamental concepts in object-oriented programming. We'll also explore practical examples to illustrate these concepts.

Core Content

What is an Enum?

An enum is a special data type that enables a variable to be a set of predefined constants. Here's a simple example:

Day.java
1public enum Day {
2 SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
3};

In this example, Day is an enum that has seven constants representing the days of the week.

Using Enums in Switch Statements

Enums are often used with switch statements to make code more readable and maintainable. Here's how you can use enums in a switch statement:

Main.java
1public class Main {
2 public static void main(String[] args) {
3 Day today = Day.MONDAY;
4
5 switch (today) {
6 case MONDAY:
7 System.out.println("It's Monday!");
8 break;
9 case TUESDAY:
10 System.out.println("It's Tuesday!");
11 break;
12 // Add cases for other days
13 default:
14 System.out.println("It's another day.");
15 break;
16 }
17 }
18}
Output
It's Monday!

Looping Through Enum Values

You can loop through all the values of an enum using the values() method, which returns an array of all the constants in the enum. Here's an example:

Main.java
1public class Main {
2 public static void main(String[] args) {
3 for (Day day : Day.values()) {
4 System.out.println(day);
5 }
6 }
7}
Output
SUNDAY
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY

Inheritance with Enums

Enums in Java can extend other classes, but they must extend the Enum class. This allows you to add methods and fields to enums. Here's an example:

Day.java
1public enum Day {
2 SUNDAY(0), MONDAY(1), TUESDAY(2), WEDNESDAY(3), THURSDAY(4), FRIDAY(5), SATURDAY(6);
3
4 private int value;
5
6 Day(int value) {
7 this.value = value;
8 }
9
10 public int getValue() {
11 return value;
12 }
13}

In this example, each enum constant has an associated integer value. You can access this value using the getValue() method.

Polymorphism with Enums

Enums can also be used in polymorphic scenarios. Here's an example where we define a base class and two derived classes:

Animal.java
1public abstract class Animal {
2 public abstract void makeSound();
3}
Dog.java
1public class Dog extends Animal {
2 @Override
3 public void makeSound() {
4 System.out.println("Woof!");
5 }
6}
Cat.java
1public class Cat extends Animal {
2 @Override
3 public void makeSound() {
4 System.out.println("Meow!");
5 }
6}

Now, let's create an enum that represents different animals and use them in a polymorphic way:

Main.java
1public class Main {
2 public static void main(String[] args) {
3 Animal myDog = new Dog();
4 Animal myCat = new Cat();
5
6 myDog.makeSound(); // Output: Woof!
7 myCat.makeSound(); // Output: Meow!
8 }
9}

Enum Constants with Methods

You can also define methods within each enum constant. Here's an example:

Day.java
1public enum Day {
2 SUNDAY("It's Sunday!") {
3 public String getAdvice() {
4 return "Relax and enjoy the weekend!";
5 }
6 },
7 MONDAY("It's Monday!") {
8 public String getAdvice() {
9 return "Start your week with energy!";
10 }
11 };
12
13 private final String message;
14
15 Day(String message) {
16 this.message = message;
17 }
18
19 public abstract String getAdvice();
20
21 @Override
22 public String toString() {
23 return message;
24 }
25}

In this example, each enum constant has a getAdvice() method that provides specific advice for that day. The toString() method is overridden to provide a custom string representation.

Practical Example

Let's create a practical example that combines enums with inheritance and polymorphism. We'll define an enum for different types of vehicles and use them in a polymorphic way.

Vehicle.java
1public abstract class Vehicle {
2 public abstract void start();
3}
Car.java
1public class Car extends Vehicle {
2 @Override
3 public void start() {
4 System.out.println("Car engine started.");
5 }
6}
Motorcycle.java
1public class Motorcycle extends Vehicle {
2 @Override
3 public void start() {
4 System.out.println("Motorcycle engine started.");
5 }
6}

Now, let's define an enum for different types of vehicles:

VehicleType.java
1public enum VehicleType {
2 CAR(new Car()),
3 MOTORCYCLE(new Motorcycle());
4
5 private final Vehicle vehicle;
6
7 VehicleType(Vehicle vehicle) {
8 this.vehicle = vehicle;
9 }
10
11 public void start() {
12 vehicle.start();
13 }
14}

Finally, let's use the enum in a main class:

Main.java
1public class Main {
2 public static void main(String[] args) {
3 VehicleType car = VehicleType.CAR;
4 VehicleType motorcycle = VehicleType.MOTORCYCLE;
5
6 car.start(); // Output: Car engine started.
7 motorcycle.start(); // Output: Motorcycle engine started.
8 }
9}

Summary

  • Enums are a special data type that allows you to define a set of predefined constants.
  • Enums can be used in switch statements for better code readability and maintainability.
  • You can loop through enum values using the values() method.
  • Enums can extend other classes, allowing you to add methods and fields.
  • Enums can be used in polymorphic scenarios, enabling more flexible and reusable code.

What's Next?

In the next tutorial, we will explore how to handle user input in Java. We'll cover different ways to read input from the console, including using Scanner, BufferedReader, and command-line arguments. This knowledge will be essential for building interactive applications that require user interaction. Stay tuned!


PreviousJava InterfaceNext Java User Input

Recommended Gear

Java InterfaceJava User Input