Member-only story
Why This Single Line of Code Made Me Question Everything About Enums in Java
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
==
andequals()
, wondering which path to take.
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.