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 276 views
2 answers 345 views
1 answer 257 views
257 views asked Nov 9, 2021 by avibootz
2 answers 271 views
271 views asked Aug 5, 2023 by avibootz
1 answer 300 views
1 answer 269 views
269 views asked Jan 16, 2022 by avibootz
...