How to get an enum value from a string in Java

2 Answers

0 votes
public class MyClass {
    public enum Color {
        RED,
        GREEN,
        BLUE
    }
    public static void main(String args[]) {
        String colorString = "RED";
        
        Color color = Color.valueOf(colorString);
 
        System.out.println(color);
    }
}
 
 
 
/*
run:
 
RED
 
*/

 



answered Nov 28, 2023 by avibootz
0 votes
public class MyClass {
    public enum Color {
        RED,
        GREEN,
        BLUE
    }
    public static void main(String args[]) {
        String colorString = "RED";
        
        Color color = Enum.valueOf(Color.class, colorString);
 
        System.out.println(color);
    }
}
 
 
 
/*
run:
 
RED
 
*/

 



answered Nov 28, 2023 by avibootz

Related questions

1 answer 198 views
2 answers 214 views
1 answer 164 views
164 views asked Nov 9, 2021 by avibootz
2 answers 153 views
153 views asked Aug 5, 2023 by avibootz
1 answer 171 views
1 answer 185 views
185 views asked Jan 16, 2022 by avibootz
...