---
title: "Convert a String to an enum in Java"  
description: "Convert a String to an enum in Java"  
author: "Samuel Fernandes"  
published: 2015-05-08  
updated: 2015-05-08  
canonical: https://www.mindstick.com/forum/23203/convert-a-string-to-an-enum-in-java  
category: "java"  
tags: ["java", "string"]  
reading_time: 1 minute  

---

# Convert a String to an enum in Java

[Say](https://answers.mindstick.com/qa/116645/a1-wreckers-reviews-what-do-customers-say) I have an enum which is just\
[public](https://www.mindstick.com/blog/11097/java-access-modifiers-the-public-and-the-private-modifiers) enum Blah { A, B, C, D}and I would like to find the [enum value](https://www.mindstick.com/forum/159623/how-to-get-an-enum-value-from-a-string-value-in-c-sharp) of a [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp), for example "A" which would be Blah.A. How would it be possible to do this?\
Is the Enum.valueOf() the [method](https://www.mindstick.com/forum/166/webservice-method) I need? If so, how would I use this?

## Replies

### Reply by Anonymous User

Yes, **Blah.valueOf("A")** will give you Blah.A.\
The static methods valueOf() and values() are created at compile time and do not appear in source code. They do appear in Javadoc, though; for example, Dialog.ModalityType shows both methods. \
You can use **[Enum](https://www.mindstick.com/forum/159626/how-can-i-cast-a-string-to-an-enum).valueOf(Blah.class, string)** as well. \
\
If you don't want to write your own utility use Google's guava library:**Enums.getIfPresent(Blah.class, "A")**


---

Original Source: https://www.mindstick.com/forum/23203/convert-a-string-to-an-enum-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
