Java How to Convert Enum to String with Examples
In Java, you can convert an enum to a string by calling its
name() method or toString() method, for example, myEnum.name() returns the enum constant's name as a string.Examples
InputDay.MONDAY
Output"MONDAY"
InputColor.RED
Output"RED"
InputSeason.SUMMER
Output"SUMMER"
How to Think About It
To convert an enum to a string, think of the enum as a fixed set of named values. Each enum constant has a name that you want to get as text. Using
name() or toString() methods gives you that name as a string, which you can use like any other text in your program.Algorithm
1
Identify the enum variable you want to convert.2
Call the <code>name()</code> method on the enum variable to get its string name.3
Use or print the returned string as needed.Code
java
public class EnumToStringExample { enum Day { MONDAY, TUESDAY, WEDNESDAY } public static void main(String[] args) { Day today = Day.MONDAY; String dayName = today.name(); System.out.println(dayName); } }
Output
MONDAY
Dry Run
Let's trace converting Day.MONDAY to string using name()
1
Assign enum value
today = Day.MONDAY
2
Call name() method
dayName = today.name() // returns "MONDAY"
3
Print the string
Output: MONDAY
| Step | Enum Value | Method Called | Result String |
|---|---|---|---|
| 1 | Day.MONDAY | name() | MONDAY |
Why This Works
Step 1: Enum constants have names
Each enum constant has a fixed name that identifies it, accessible via name().
Step 2: name() returns the exact enum name
name() returns the enum constant's name as a string exactly as declared.
Step 3: toString() can be overridden
toString() also returns a string but can be customized, while name() is fixed.
Alternative Approaches
Using toString() method
java
public class EnumToStringAlt { enum Color { RED, GREEN, BLUE } public static void main(String[] args) { Color c = Color.RED; System.out.println(c.toString()); } }
toString() returns the enum name by default but can be overridden for custom strings.
Custom method in enum
java
public class EnumCustomString { enum Season { WINTER("Cold"), SUMMER("Hot"); private String desc; Season(String desc) { this.desc = desc; } public String getDesc() { return desc; } } public static void main(String[] args) { System.out.println(Season.WINTER.getDesc()); } }
You can add a method to return a custom string instead of the enum name.
Complexity: O(1) time, O(1) space
Time Complexity
Calling name() or toString() is a direct method call returning a stored string, so it runs in constant time.
Space Complexity
No extra memory is allocated besides the returned string reference, so space usage is constant.
Which Approach is Fastest?
name() and toString() have similar performance; name() is safer for exact enum names.
| Approach | Time | Space | Best For |
|---|---|---|---|
| name() | O(1) | O(1) | Exact enum name string |
| toString() | O(1) | O(1) | Customizable string representation |
| Custom method | O(1) | O(1) | Custom descriptive strings |
Use
name() for the exact enum name string and toString() if you want a customizable string.Confusing
name() with toString() and expecting toString() to always return the enum name.