package net.viralpatel.java.enum;
public enum Weekdays {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
Code language: Java (java)
Now we want to get an instance of Weekdays enum from string values lets say “Monday”, “Tuesday” etc. We can get this as follow: Weekdays weekday = Weekdays.valueOf("Monday");
System.out.println(weekday);
Code language: Java (java)
Output: One thing we need to take care here is if we pass an invalid string to valueOf() method like “XYZ”, the method will give a runtime exception.Monday
Weekdays weekday = Weekdays.valueOf("XYZ");
System.out.println(weekday);
Code language: Java (java)
Output: Exception in thread "main" java.lang.IllegalArgumentException:
No enum const class net.viralpatel.java.enum.Weekdays.XYZ
at java.lang.Enum.valueOf(Enum.java:192)
Code language: Java (java)
Happy converting :) Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
It is a bad practice to do this. Basically, your client code will have a fragile dependency on the name of an enum encoded as a string. it's a variation of the "stringly typed system" smell.
Read more here: http://canoo.com/blog/2010/09/24/beautiful-java-enums/
Tq.. it was helpful :)