valueOf Example inward Java Enum
valueOf method of Java Enum is used to recollect Enum constant declared inward Enum Type past times passing String inward other words valueOf method is used to convert String to Enum constants. In this Java Enum valueOf instance nosotros volition come across how to role valueOf method inward Java. valueOf method is implicitly available to all Java Enum because every enum inward Java implicitly extends java.lang.Enum class. valueOf method of enum accepts just same String which is used to declare Enum constant to supply that Enum constant. valueOf method is case-sensitive as well as invalid String volition lawsuit inward IllegalArgumentException. In curt String passed to valueOf method of Java enum must last same to String returned past times name() method e.g. TrafficSigal.RED.name() returns RED as well as yous should transcend RED to valueOf() to larn TrafficSignal.RED. This volition last to a greater extent than clear volition next Java Enum valueOf example. By the agency its 3rd tutorial on Enum inward Java afterward previous ship on Java Enum Swith Example and Java Enum toString example. If yous receive got non read those tutorial yous may honour useful.
valueOf Java Enum Example
Here is consummate code instance of valueOf method of Java Enum. In this enum instance nosotros receive got used a TrafficSignal Enum to demonstrate how nosotros tin dismiss recollect Enum from String inward Java.

*
* Java plan to demo How to role valueOf method of Enum
* to exercise enum. This is uncomplicated Enum valueOf instance which
* teaches how to role valueOf method.
*
* @author
*/
public class EnumValueOfExample {
public static void main(String args[]) {
//valueOf method returns Enum instance amongst advert matching to String passed to valueOf
TrafficSignal request = TrafficSignal.valueOf("RED");
System.out.println("name : " + signal.name() + " activeness : " + signal.getAction());
//Another Enum valueOf exampel
request = TrafficSignal.valueOf("GREEN");
System.out.println("name : " + signal.name() + " activeness : " + signal.getAction());
//valueOf volition throw IllegalArgumentException if nosotros transcend invalid String
request = TrafficSignal.valueOf("Green");
}
}
enum TrafficSignal{
RED("stop"), GREEN("start"), ORANGE("slow down");
private String action;
public String getAction(){
return this.action;
}
private TrafficSignal(String action){
this.action = action;
}
}
Output:
advert : RED activeness : stop
advert : GREEN activeness : start
Exception inward thread "main" java.lang.IllegalArgumentException: No enum const class test.TrafficSignal.Green
at java.lang.Enum.valueOf(Enum.java:196)
at test.TrafficSignal.valueOf(EnumValueOfExample.java:34)
at test.CollectionTest.main(EnumValueOfExample.java:29)
Java Result: 1
That's all on this Java Enum valueOf example. See 10 Java enum Examples for to a greater extent than examples of enum inward Java. In curt enum valueOf is a useful method which is past times default available to all enum constants as well as tin dismiss last used to larn Enum constants from String.
Further Learning
Complete Java Masterclass
Java CyclicBarrier Example - Concurrency tutorial
No comments:
Post a Comment