enum to string java

Solutions on MaxInterview for enum to string java by the best coders in the world

showing results for - "enum to string java"
Elias
09 Jul 2019
1private enum Status
2{
3    Awesome = 1,
4    Cool = 2
5};
6
7public string ConvertEnumToString(Status status)
8{
9   return status.ToString();
10}
11
12public Status ConvertStringToEnum(string status)
13{
14    return (Status) Enum.Parse(typeof (Status), status);
15}
16
17//serializer.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
18
19//Fast enum
20public enum HumanStates
21{
22	Idle,
23    Working,
24    Sleeping,
25    Eating,
26    Dead
27     
28}
29
30public string FastToString(HumanStates state)
31{
32	switch(state)
33    {
34      case HumanStates.Idle:
35        	return nameof(HumanStates.Idle);
36      case HumanStates.Working:
37        	return nameof(HumanStates.Working);
38      case HumanStates.Sleeping:
39        	return nameof(HumanStates.Sleeping);
40      case HumanStates.Eating:
41        	return nameof(HumanStates.Eating);
42      case HumanStates.Dead:
43        	return nameof(HumanStates.Dead);
44      default:
45        throw new ArgumentOutOfRangeException(nameof(state), state, null);        
46    }
47}
Arianna
20 Nov 2016
1public enum MyEnum{
2	SOMETHING,SOMETHING_ELSE
3}
4MyEnum enumObject=MyEnum.SOMETHING;
5System.out.println(enumObject.toString());
6System.out.println(enumObject.name());
Darryl
30 Jul 2016
1    String name = Modes.mode1.name(); // Returns the name of this enum constant, exactly as declared in its enum declaration.
2