January 29, 2010

Java enums from a given string

0  comments

To generate a instance of a enum class by a given string you can use the valueOf() method.

public class EnumByGivenStringSample {
    private enum Language
       {
           JAVA, RUBY, SCALA, GROOVY, CLOJURE
       }

       public static Language languageByString(String language)
       {
           return Language.valueOf(language);
       }
}

Tags

enum, Java, Snippets


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked

Information about Data protection

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

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}