How to Retrieve a Java Enum Using Its String Value?

Question

How can I look up a Java enum by its String value?

public enum Verbosity {
    BRIEF, NORMAL, FULL;

    private static Map<String, Verbosity> stringMap = new HashMap<>();

    static {
        for (Verbosity verbosity : Verbosity.values()) {
            stringMap.put(verbosity.name(), verbosity);
        }
    }

    public static Verbosity getVerbosity(String key) {
        return stringMap.get(key);
    }
}

Answer

Retrieving a Java enum using its String representation can be accomplished by utilizing a static map to store and quickly look up enum instances.

public enum Verbosity {
    BRIEF, NORMAL, FULL;

    private static Map<String, Verbosity> stringMap = new HashMap<>();

    static {
        for (Verbosity verbosity : Verbosity.values()) {
            stringMap.put(verbosity.name(), verbosity);
        }
    }

    public static Verbosity getVerbosity(String key) {
        return stringMap.get(key);
    }
}

Causes

  • The code provided uses an instance initializer, which is not suitable because it tries to use static context within an instance method.
  • Static initialization blocks must be used to initialize static members correctly.

Solutions

  • Use a static initializer block to populate the map when the enum class is loaded.
  • Replace `this.toString()` with `this.name()` which is more common for enum constants.

Common Mistakes

Mistake: Using `this.toString()` instead of `this.name()` in the enum context.

Solution: Use `this.name()` to get the string representation of the enum constant.

Mistake: Not initializing the static map properly in the enum.

Solution: Utilize a static block to populate the `stringMap`.

Helpers

  • Java enum
  • lookup enum by string
  • Java get enum from string
  • enum static initializer
  • Java enums best practices

Related Questions

⦿Understanding the Differences Between Period, Interval, and Duration in Joda-Time

Learn the key differences between Period Interval and Duration in JodaTime including their use cases and performance implications.

⦿What is the Source and Purpose of the values() Method in Java Enums?

Discover the origin and function of the values method in Java enums including example usage and common mistakes.

⦿How to Securely Hash Passwords in Java for Database Storage

Learn how to hash passwords in Java using secure methods with salt for safe database storage. Follow our stepbystep guide

⦿Understanding Downcasting in Java: Practical Applications and Considerations

Explore why Java allows downcasting even if it can lead to runtime exceptions. Learn about its applications risks and best practices.

⦿How to Determine the Type of a Variable in Java?

Learn how to check and verify the type of a variable in Java with examples and tips for best practices.

⦿How to Clone an InputStream in Java?

Learn how to clone an InputStream in Java to protect against closure by external libraries. Explore relevant code examples and common pitfalls.

⦿Does Using Java Reflection to Create Objects Impact Performance Compared to Class Constructor Calls?

Explore the performance differences between using Java Reflection for object creation and traditional class constructor calls including drawbacks and solutions.

⦿What are the Key Differences Between C#, Java Generics, and C++ Templates?

Explore the differences between C generics Java generics and C templates including pros and cons of each. Learn how they compare in performance and type safety.

⦿Fixing 'Fatal Error Compiling: Invalid Target Release: 1.8' in Maven Projects

Learn how to resolve the Fatal Error Compiling Invalid Target Release 1.8 issue in Maven projects with stepbystep guidance and code examples.

⦿How to Toggle Between Camel Case and Underscore Spacing in IntelliJ?

Learn how to switch between camel case and underscore naming conventions in IntelliJ IDEA. Discover shortcuts plugins and solutions.

© Copyright 2025 - CodingTechRoom.com

close