Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Enum values as String literals

Tags:

java

string

enums

What is the best way to use the values stored in an Enum as String literals? For example:

public enum Modes {     some-really-long-string,     mode1,     mode2,     mode3 } 

Then later I could use Mode.mode1 to return its string representation as mode1. Without having to keep calling Mode.mode1.toString().

like image 756
Larry Avatar asked Jul 12 '11 15:07

Larry


People also ask

Can enum values be strings?

However, enum values are required to be valid identifiers, and we're encouraged to use SCREAMING_SNAKE_CASE by convention. Given those limitations, the enum value alone is not suitable for human-readable strings or non-string values.

Can enum store string?

Enum constants can only be of ordinal types ( int by default), so you can't have string constants in enums.

Can enum values be string C++?

Here we will see how to map some enum type data to a string in C++. There is no such direct function to do so. But we can create our own function to convert enum to string. We shall create a function that takes an enum value as the argument, and we manually return the enum names as a string from that function.

How do I convert an enum to a string in Java?

There are two ways to convert an Enum to String in Java, first by using the name() method of Enum which is an implicit method and available to all Enum, and second by using toString() method.


2 Answers

You can't. I think you have FOUR options here. All four offer a solution but with a slightly different approach...

Option One: use the built-in name() on an enum. This is perfectly fine if you don't need any special naming format.

    String name = Modes.mode1.name(); // Returns the name of this enum constant, exactly as declared in its enum declaration. 

Option Two: add overriding properties to your enums if you want more control

public enum Modes {     mode1 ("Fancy Mode 1"),     mode2 ("Fancy Mode 2"),     mode3 ("Fancy Mode 3");      private final String name;             private Modes(String s) {         name = s;     }      public boolean equalsName(String otherName) {         // (otherName == null) check is not needed because name.equals(null) returns false          return name.equals(otherName);     }      public String toString() {        return this.name;     } } 

Option Three: use static finals instead of enums:

public final class Modes {      public static final String MODE_1 = "Fancy Mode 1";     public static final String MODE_2 = "Fancy Mode 2";     public static final String MODE_3 = "Fancy Mode 3";      private Modes() { } } 

Option Four: interfaces have every field public, static and final:

public interface Modes {      String MODE_1 = "Fancy Mode 1";     String MODE_2 = "Fancy Mode 2";     String MODE_3 = "Fancy Mode 3";   } 
like image 174
Michael J. Lee Avatar answered Sep 28 '22 02:09

Michael J. Lee


Every enum has both a name() and a valueOf(String) method. The former returns the string name of the enum, and the latter gives the enum value whose name is the string. Is this like what you're looking for?

String name = Modes.mode1.name(); Modes mode = Modes.valueOf(name); 

There's also a static valueOf(Class, String) on Enum itself, so you could also use:

Modes mode = Enum.valueOf(Modes.class, name); 
like image 40
Ryan Stewart Avatar answered Sep 28 '22 01:09

Ryan Stewart