Java Enum To String

Java Enums is a new feature introduced in Java 5. Using Java Enum can help define a collection of constants in a more logical and meaningful way. Using Java Enum is a fluent way of defining constants that makes things more readable and maintainable. But sometimes we also need to convert a Java Enum to String, and that is what we will show in the examples below:

Review of Enum

Let's review first why we want to use enum. When we can to have a group of constants, say vehicle type, we will do something like this:
/**
 * A simple example of a class with constants.
 */
public class Vehicle {
    public static final int BIKE = 1;
    public static final int BUS = 2;
    public static final int CAR = 3;
    public static final int TRUCK = 4;
}
This is non intuitive and prone to error. For one, we can mistakenly assign the same literal value to two different constants. For example, we may have something like:
/**
 * A simple example of a class with constants with error.
 */
public class Vehicle {
    public static final int BIKE = 1;
    public static final int BUS = 2;
    public static final int CAR = 3;
    public static final int TRUCK = 1;
}
Where BIKE and TRUCK have the same value of 1. This is important when we have long list of constant and it's hard to keep track. How do we translate this to enum? Easy, just like this:
/**
 * A simple example of Enums representing types of Vehicles.
 */
public enum Vehicle {
    BIKE, BUS, CAR, TRUCK;
}
Which is simple, intuitive, fluent, and less prone to errors. This is very desirable to use in our codes.

Java Enum To String Using name()

Given the Vehicle enum defined above, let's solve the problem on how to convert this to String. One way to do that is to invoke the name() method of an enum. Below is an example on how to do this.

/**
 * A simple example that converts an enum to string using name()
 */
public class TestEnumExample {
    public static void main(String[] args) {
        System.out.println(Vehicle.BIKE.name());
        System.out.println(Vehicle.BUS.name());
        System.out.println(Vehicle.CAR.name());
        System.out.println(Vehicle.TRUCK.name());
    }
}

The name() method will return the name we used for each type in the enum in String format. Below is the output of the code above:

BIKE
BUS
CAR
TRUCK

Java Enum To String Using toString()

Since each type of an enum is an instance of the Enum class, we can invoke the toString method. Remember that all classes are derived from Object, hence all instance of any class have a toString method that converts the object to it's String representation. Below is a modified example that uses toString method:

/**
 * A simple example that converts an enum to string using toString()
 */
public class TestEnumExample {
    public static void main(String[] args) {
        System.out.println(Vehicle.BIKE.toString());
        System.out.println(Vehicle.BUS.toString());
        System.out.println(Vehicle.CAR.toString());
        System.out.println(Vehicle.TRUCK.toString());
    }
}

And the output is the same as expected:

BIKE
BUS
CAR
TRUCK

Java Enum To String Using Member Property

Another way is we could control the String representation and have finer control by using a property of the enum. Below is an example modification of our Vehicle enum.

/**
 * A simple example of Enums representing types of Vehicles with a property.
 */
public enum Vehicle {
    BIKE("Bicycle"), BUS("Big Bus"), CAR("Nice Sedan Car"), TRUCK("Big Truck");
    String longName;
    Vehicle(String longName) {
        this.longName = longName;
    }
    public String getLongName() {
        return longName;
    }
}

And we can invoke the getLongName() method in our program to get the String representation of an Enum.
/**
 * A simple example that converts an enum to string using custom property
 */
public class TestEnumExample {
    public static void main(String[] args) {
        System.out.println(Vehicle.BIKE.getLongName());
        System.out.println(Vehicle.BUS.getLongName());
        System.out.println(Vehicle.CAR.getLongName());
        System.out.println(Vehicle.TRUCK.getLongName());
    }
}

And this is a nice way to convert an enum to String in Java with some customization. Below is the expected output:

Bicycle
Big Bus
Nice Sedan Car
Big Truck