Enum Constructor example

📘 Java Enums with Constructors — A Simple & Clear Guide

Enums in Java are often misunderstood as simple lists of constants.
But Enums are actually powerful types — they can include fields, methods, and even constructors.

In this blog, we’ll explore how to use Enum constructors to attach data to each enum constant.
We’ll also walk through an example where each subject has a maximum mark.


✅ What Are Enums in Java?

enum stands for enumeration, and it represents a fixed set of constants.

Example:

enum Color { RED, GREEN, BLUE }

But Java enums are much more than constants.
They are full-fledged classes with:

  • Variables (fields)

  • Methods

  • Constructors


🎯 Why Use Constructors in Enums?

Sometimes, each enum constant needs to hold some extra information.

For example:

  • Days of week → working hours

  • Planets → mass, radius

  • Subjects → maximum marks

By using a constructor inside an enum, we can store additional data for each constant.


🧩 Example Use Case: Subjects and Maximum Marks

Suppose you want to store maximum marks for each subject:

SubjectCodeMaximum Mark
PhysicsP50
ChemistryC50
BiologyB75
MathM100

Instead of using separate variables or maps, we attach values directly to enum constants.


🧱 Text Diagram: How Enum with Constructor Works

┌───────────────────────────────┐ │ enum MaxMark │ ├───────────────────────────────┤ │ P(50) → maxmark = 50 │ │ C(50) → maxmark = 50 │ │ B(75) → maxmark = 75 │ │ M(100) → maxmark = 100 │ ├───────────────────────────────┤ │ getMaxmark() returns value │ └───────────────────────────────┘

Each constant calls the constructor with a different value.


🧑‍💻 Full Java Code Example

package com.vinod.test; /** * Example showing Java Enum with Constructor * Author: Vinod Kariyathungal Kumaran */ public class EnumConstructorExample { public static void main(String[] args) { // Print all subjects and their maximum marks for (MaxMark m : MaxMark.values()) { System.out.println("Subject code=" + m + " Maximum Mark " + m.getMaxmark()); } // Print a specific subject's maximum mark System.out.println("Physics Maximum Mark=" + MaxMark.P.getMaxmark()); } } enum MaxMark { P(50), C(50), B(75), M(100); private int maxmark; // Enum constructor MaxMark(int p) { maxmark = p; } // Getter method int getMaxmark() { return maxmark; } }

🟦 Explanation

🔹 1. Enum Constants with Values

P(50), C(50), B(75), M(100)

Each constant calls the constructor with a number (max mark).

🔹 2. Constructor

MaxMark(int p) { maxmark = p; }

This assigns the passed value to the internal variable.

🔹 3. Getter Method

int getMaxmark() { return maxmark; }

Used to read the stored value.


🖨️ Program Output

Subject code=P Maximum Mark 50 Subject code=C Maximum Mark 50 Subject code=B Maximum Mark 75 Subject code=M Maximum Mark 100 Physics Maximum Mark=50

🎉 Final Thoughts

Java Enums are more powerful than most people realize.
By adding constructors and fields, you can treat them like lightweight data holders — clean, readable, and type-safe.

This approach is perfect for:

✔ Subject mark mapping
✔ Error codes with descriptions
✔ HTTP status codes
✔ Pricing tiers
✔ Role definitions


No comments:

Post a Comment

12 classic String-based Java interview questions with simple explanations and code.

  1️⃣ Check if a String is a Palindrome Problem Given a string, check if it reads the same forward and backward. Example: "madam...

Featured Posts