Java Enum Strings: Harnessing Constants for Clarity in Code
Discover the power of Java enums as a special construct for defining groups of predefined constant strings. Learn how to effectively use enums to improve code clarity and maintainability. Explore practical examples to see how enums enhance your Java applications, ensuring a clean and organized approach to managing constant values.
Java - Enum String
What is an Enum in Java?
Java enum is a special construct that represents a group of pre-defined constant strings, providing clarity in code when used as constants in application code. By default, an enum's string representation is the same as its declaration. Consider the following example:
WEEKDAY Enum Example
enum WEEKDAY {
MONDAY, TUESDAY, WEDNESDAY, THRUSDAY, FRIDAY, SATURDAY, SUNDAY
}
If we print the string representation of the above enum using the enum directly, using toString()
, or using the name()
method, it will print the same string as declared:
Print Enum String Representation
System.out.println(WEEKDAY.MONDAY);
System.out.println(WEEKDAY.MONDAY.toString());
System.out.println(WEEKDAY.MONDAY.name());
Output
MONDAY
MONDAY
MONDAY
Overriding Enum toString() Method
If we want to change the default string representation of the enum, we can create an overridden toString()
method for each value of the enum constructor, as shown below:
WEEKDAY Enum with Overridden toString() Method
enum WEEKDAY {
MONDAY {
// Overridden toString() method per value
public String toString() {
return "Day 1 of the Week: Monday";
}
};
// Or override toString() per enum
// Priority will be given to value level toString() method.
public String toString() {
return "Day 1 of the Week: Monday";
}
}
Example: Overriding toString() Method in Java
In this example, we've created an enum WEEKDAY
. Using the toString()
method, we're setting a custom description for each enum value:
WEEKDAY Enum Example
package com.tutorialsarena;
enum WEEKDAY {
// Enum value constants
MONDAY, TUESDAY, WEDNESDAY, THRUSDAY, FRIDAY, SATURDAY, SUNDAY;
// Override the toString() method for custom description
@Override
public String toString() {
return switch(this) {
case MONDAY: yield "Day 1";
case TUESDAY: yield "Day 2";
case WEDNESDAY: yield "Day 3";
case THRUSDAY: yield "Day 4";
case FRIDAY: yield "Day 5";
case SATURDAY: yield "Day 6";
case SUNDAY: yield "Day 7";
};
}
}
public class Tester {
public static void main(String[] args) {
// Invoke toString() internally
System.out.println(WEEKDAY.MONDAY);
// Invoke toString explicitly
System.out.println(WEEKDAY.TUESDAY.toString());
// Invoke name() method to get the default name
System.out.println(WEEKDAY.WEDNESDAY.name());
}
}
Output
Day 1
Day 2
WEDNESDAY
Example: Overriding toString() Method Per Value in Java
In this example, we've overridden the toString()
methods for each value of the WEEKDAY
enum. This allows us to customize the string representation for each value:
WEEKDAY Enum Example with Per Value toString()
package com.tutorialsarena;
enum WEEKDAY {
// Override the toString() method for custom description
MONDAY {
@Override
public String toString() {
return "Day 1";
}
},
TUESDAY {
@Override
public String toString() {
return "Day 2";
}
},
WEDNESDAY {
@Override
public String toString() {
return "Day 3";
}
},
THRUSDAY {
@Override
public String toString() {
return "Day 4";
}
},
FRIDAY {
@Override
public String toString() {
return "Day 5";
}
},
SATURDAY {
@Override
public String toString() {
return "Day 6";
}
},
SUNDAY {
@Override
public String toString() {
return "Day 7";
}
};
}
public class Tester {
public static void main(String[] args) {
// Invoke toString() internally
System.out.println(WEEKDAY.MONDAY);
// Invoke toString explicitly
System.out.println(WEEKDAY.TUESDAY.toString());
// Invoke name() method to get the default name
System.out.println(WEEKDAY.WEDNESDAY.name());
}
}
Output
Day 1
Day 2
WEDNESDAY
The name()
method of the enum is final and cannot be overridden. It can be used to get the default name of the enum, while the string representation of the enum is overridden by the toString()
method.