Nearby lessons

49 of 125

Java - Enums

📌 What You Will Learn
  • What class and object mean, with real-life examples
  • The four pillars: Encapsulation, Abstraction, Inheritance, Polymorphism
  • Constructors, this, super, static and final
  • Method overloading and method overriding (with a comparison table)
  • Abstract classes and interfaces (including the modern changes)
  • Association, Aggregation and Composition

Enums is a core concept of the Java language. This lesson explains Enums — Fixed Set of Constants with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Enums — Fixed Set of Constants

An enum is a special kind of class that stores a fixed set of constant values — like the days of a week or the colours of a traffic light. It was added in Java 5 and is perfect for values that never change.

Important facts about enums:

  • Every enum is a final class that automatically extends java.lang.Enum.
  • Its constants are public static final — accessed like Day.MONDAY.
  • An enum is a full class: it can have fields, constructors, methods and even implement interfaces.
  • switch works perfectly with enums, making menu-driven code clean and safe.
Example01
JCode Cell
1enum Day {
2 MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
3}
4 
5class Test {
6 public static void main(String[] args) {
7 Day today = Day.MONDAY;
8 System.out.println(today); // MONDAY
9 
10 for (Day d : Day.values()) { // all constants
11 System.out.print(d + " ");
12 }
13 System.out.println();
14 
15 System.out.println(Day.valueOf("FRIDAY")); // String -> enum
16 System.out.println(Day.WEDNESDAY.ordinal()); // position (starts 0): 2
17 }
18}
Output
MONDAY MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY FRIDAY 2

Enums — Fixed Set of Constants

Trainer's Note: Use an enum whenever you have a small, known list of values (status, type, category). It is safer than String constants because the compiler checks the values — typos are caught at compile time, not at run time.
Example02
JCode Cell
1enum PizzaSize {
2 SMALL(10), MEDIUM(14), LARGE(18); // each size has a price
3 
4 private final int price;
5 PizzaSize(int price) { this.price = price; } // enum constructor
6 int getPrice() { return price; }
7}
8 
9class Test {
10 public static void main(String[] args) {
11 System.out.println("Medium pizza: " + PizzaSize.MEDIUM.getPrice());
12 }
13}
Output
Medium pizza: 14
📝 Key Takeaways
  • Class = blueprint; Object = real thing made from it (memory in heap).
  • Four pillars: Encapsulation (hide data), Abstraction (show only essentials), Inheritance (reuse parent code), Polymorphism (one name, many forms).
  • Overloading = same class, different parameters, decided at compile time. Overriding = child redefines parent method, decided at run time.
  • Constructor initialises the object; name = class name, no return type.
  • this = current object; super = parent class; static = belongs to class; final = cannot change.
  • abstract class cannot be instantiated; interface is a contract a class implements.
  • Association (weak), Aggregation (has-a, part independent), Composition (strong has-a, part dies with whole).

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1