Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

1. Introduction

In this short tutorial, we'll learn how to quickly convert a String into an enum in Java.

2. Setup

We're dealing with core Java, so we don't need to add any additional artifacts. We'll also be working with the PizzaDeliveryStatusEnum from the enums guide article.

3. The Conversion

Enums are similar to standard Java classes, and we can access their values using the dot notation. So to access the READY value of PizzaDeliveryStatusEnum, we would use:

PizzaStatusEnum readyStatus = PizzaStatusEnum.READY;

This is fine, but what if we had the value of the status stored as a String, and wanted to convert it into a PizzaStatusEnum? The naive way of doing this would be to write a giant switch statement, returning the correct value of the enum for each of its possible values. But writing and maintaining such code is a nightmare, and we should avoid it at all costs.

On the other hand, the enum type provides a valueOf() method that takes a String as an argument, and returns the corresponding enum object:

PizzaStatusEnum readyStatus = PizzaStatusEnum.valueOf("READY");

We can check that this approach actually works through a unit test:

@Test
public void whenConvertedIntoEnum_thenGetsConvertedCorrectly() {
 
    String pizzaEnumValue = "READY";
    PizzaStatusEnum pizzaStatusEnum
      = PizzaStatusEnum.valueOf(pizzaEnumValue);
    assertTrue(pizzaStatusEnum == PizzaStatusEnum.READY);
}

It's important to remember that the valueOf() method does a case-sensitive match of the argument supplied to it, so passing a value that doesn't match the case of any of the original enum‘s values will lead to an IllegalArgumentException:

@Test(expected = IllegalArgumentException.class)
public void whenConvertedIntoEnum_thenThrowsException() {
    
    String pizzaEnumValue = "rEAdY";
    PizzaStatusEnum pizzaStatusEnum
      = PizzaStatusEnum.valueOf(pizzaEnumValue);
}

Passing a value that's not part of the original enum‘s values also leads to an IllegalArgumentException:

@Test(expected = IllegalArgumentException.class)
public void whenConvertedIntoEnum_thenThrowsException() {
    String pizzaEnumValue = "invalid";
    PizzaStatusEnum pizzaStatusEnum = PizzaStatusEnum.valueOf(pizzaEnumValue);
}

4. Conclusion

In this brief article, we illustrated how to convert a String into an enum.

We highly recommend using the built-in valueOf() method of the enum type, instead of doing the conversion ourselves.

As always, the code for this article can be found over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Generic footer banner
Comments are closed on this article!