How to Convert Enum to String in Java

The Java programming language allows programs to define their own enumerated types. An object of an enumerated type contains a value drawn from a finite number of constants specified by the program. For example, you can define an enumerated type for the suit of a playing card. That enumerated type contains four elements: Club, Diamond, Heart and Spade. You can convert a Java enumerated object to the String representing the current value of the object.

Instructions

    • 1

      Declare an enumerated type as in the following sample code:

      public enum Suit {

      Club, Diamond, Heart, Spade

      }

    • 2

      Declare and initialize an object of the enumerated type as in the following sample code:

      Suit myCardsSuit = Diamond;

    • 3

      Convert the enumerated value to the corresponding string by calling the built-in

      name() method as in the following sample code:

      String myCardsSuitString = myCardsSuit.name();

      For the example, String myCardsSuitString will have value "Diamond".

Related Searches:

References

Comments

You May Also Like

  • How to Convert INT to String in Java

    Converting an integer to a string is a common practice when programming. For some application processes, it's necessary to manipulate the format....

  • How to Convert a String to an Enum

    C# is a programming language used for building various enterprise level applications that run on the .Net Framework. C# is simpler and...

  • How to Convert String to Date Format in Java

    The Java programming language is well-known for incorporating object orientated programming from the start. Everything in Java is based on the object...

  • How to Convert String to Date Objects

    String conversions provide a way for a software developer to switch data types in website code. The conversion from a string to...

  • Java 1.5 Enum Tutorial

    The enum type is a new feature in the Java 1.5.0 language, designed to solve weaknesses in the way enumerated values were...

Related Ads

Featured