Core Java

Enum to String Java

In this article, we’ll explore how to convert Java enum to string in a variety of ways, including using the toString() method and the name() method. We’ll also cover how to customize the string representation of an enum constant using the toString() method.

1. Introduction

Java is a powerful object-oriented programming language that offers various tools and methods to manage data types. One of the most useful features is the ability to convert an enumerated data type into a string. This process can be particularly useful when working with Java enums. In this article, we’ll explore the process of converting Java enums to strings.

2. What is an Enum in Java?

An enum (short for “enumeration”) is a special type of data type in Java that is used to define a set of constants. Enums are declared using the enum keyword, and their constants are separated by commas. Each constant is assigned a name and a value, and the name must be unique within the enum.

Here’s an example of how to declare an enum in Java:

public enum DayOfWeek {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}

In this example, we have an enum named DayOfWeek that defines the days of the week as constants.

3. Why Convert Enum to String?

Enums are useful for representing a fixed set of values, but sometimes we need to represent these values as strings. For example, we may want to display the day of the week in a user-friendly way, such as “Monday” instead of “MONDAY”. Converting an enum to a string can also be useful for logging and debugging purposes.

4. Converting Enum to String in Java

Converting an enum to a string in Java is a straightforward process. There are two main methods to do this: using the toString() method and using the name() method.

4.1 Using the “toString()” Method

The toString() method is a method that is automatically generated for every enum in Java. It returns the name of the enum constant as a string.

Here’s an example of how to use the toString method to convert an enum to a string:

        DayOfWeek day = DayOfWeek.MONDAY;
        String dayStringtoString = day.toString();
        System.out.println("Method toString() : "+dayStringtoString); // Output: MONDAY

In this example, we declare a variable named day of type DayOfWeek and assign it the value of “MONDAY”. We then use the toString() method to convert the day variable to a string and store the result in the dayString variable. Finally, we print out the dayString variable to the console, which outputs “MONDAY”.

Fig. 1: toString() Method.
Fig. 1: toString() Method.

Note that the toString() method can be overridden to return a custom string representation of an enum constant.

4.2 Using the “name()” Method

The name() method is another method that is automatically generated for every enum in Java. It returns the name of the enum constant as a string.

Here’s an example of how to use the name() method to convert an enum to a string:

        day = DayOfWeek.FRIDAY;
        String dayStringName = day.name();
        System.out.println("Method name() : "+dayStringName); // Output: FRIDAY

In this example, we declare a variable named day of type DayOfWeek and assign it the value of “MONDAY”. We then use the name() method to convert the day variable to a string and store the result in the dayString variable. Finally, we print out the dayString variable to the console, which outputs “MONDAY”.

Fig. 2: name() Method.
Fig. 2: name() Method.

Note that the name() method should be used with caution, as it is case-sensitive and can lead to errors if the case of the enum constant is not consistent.

5. Customizing String Representations of Enum

Sometimes, we may want to customize the string representation of an enum constant. For example, we may want to display the day of the week as a full name instead of an abbreviation. To do this, we can override the toString() method of the enum.

Here’s an example of how to customize the string representation of an enum constant using the toString() method:

public enum DayOfWeek {
    MONDAY("Monday"),
    TUESDAY("Tuesday"),
    WEDNESDAY("Wednesday"),
    THURSDAY("Thursday"),
    FRIDAY("Friday"),
    SATURDAY("Saturday"),
    SUNDAY("Sunday");

    private String fullName;

    private DayOfWeek(String fullName) {
        this.fullName = fullName;
    }

    @Override
    public String toString() {
        return fullName;
    }
}

In this example, we’ve added a fullName field to the DayOfWeek enum, which stores the full name of each day of the week. We’ve also added a constructor that takes a fullName parameter and assigns it to the fullName field.

Finally, we’ve overridden the toString() method to return the fullName field instead of the default name of the enum constant. This allows us to customize the string representation of the enum constant.

Now, when we convert a DayOfWeek enum constant to a string using the toString() method, we get the full name of the day instead of the abbreviation:

Fig. 3: Enum to String Customized.
Fig. 3: Enum to String Customized.

6. Conclusion

In this article, we’ve explored the process of converting Java enums to strings. We’ve covered two main methods for doing this: using the toString() method and using the name() method. We’ve also discussed how to customize the string representation of an enum constant using the toString() method.

Converting enums to strings is a powerful feature in Java that can be particularly useful when working with enums. By understanding how to convert enums to strings and customize their string representation, you can enhance the readability and usability of your Java code.

7. Download the Source Code

Download
You can download the full source code of this example here: Enum to String Java

Odysseas Mourtzoukos

Mourtzoukos Odysseas is studying to become a software engineer, at Harokopio University of Athens. Along with his studies, he is getting involved with different projects on gaming development and web applications. He is looking forward to sharing his knowledge and experience with the world.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button