Member-only story

Why This Single Line of Code Made Me Question Everything About Enums in Java

Sivaram Rasathurai
Javarevisited
Published in
6 min readFeb 21, 2025

My article is open to Every one:
If you are blocked by a medium paywall Read it
here.

I spent around 10 minutes debugging a test failure, Finally, I discovered the culprit was a single line:

if (color.equals(Color.RED)) { ... }  

The test passed when I changed it to ==.

Staring at == and equals(), wondering which path to take.

Photo by Nachristos on Unsplash

Enums

Enums are like immortal twins — each born once, forever recognizable by their face, not their name.

Each enum constant is an instance of the enum type, and these instances are singletons. This means that there is only one instance of each enum constant in the JVM, which has important implications for comparison.

public enum Color { RED, GREEN, BLUE }

Here, RED isn’t just a value. It’s a singular being, etched into memory when the JVM wakes. No clones, no imposters. Just one RED, one GREEN, one BLUE—eternal, unchanging.

This truth changes everything about how we compare them.

Comparing Enums with ==

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Javarevisited
Javarevisited

Published in Javarevisited

A humble place to learn Java and Programming better.

Responses (2)

Write a response