Java Enum Valueof Illustration - How To Role

valueOf Example inwards Java Enum
valueOf method of Java Enum is used to recollect Enum constant declared inwards Enum Type past times passing String inwards other words valueOf method is used to convert String to Enum constants. In this Java Enum valueOf illustration nosotros volition encounter how to move valueOf method inwards Java. valueOf method is implicitly available to all Java Enum because every enum inwards Java implicitly extends java.lang.Enum class. valueOf method of enum accepts just same String which is used to declare Enum constant to furnish that Enum constant. valueOf method is case-sensitive as well as invalid String volition lawsuit inwards IllegalArgumentException. In curt String passed to valueOf method of Java enum must live on same to String returned past times name() method e.g. TrafficSigal.RED.name() returns RED as well as you lot should move past times RED to valueOf() to larn TrafficSignal.RED. This volition live on to a greater extent than clear volition next Java Enum valueOf example. By the agency its 3rd tutorial on Enum inwards Java subsequently previous post on Java Enum Swith Example  and Java Enum toString example. If you lot receive got non read those tutorial you lot may uncovering useful.



valueOf Java Enum Example

Here is consummate code illustration of valueOf method of Java Enum. In this enum illustration nosotros receive got used a TrafficSignal Enum to demonstrate how nosotros tin dismiss recollect Enum from String inwards Java.



 method of Java Enum is used to recollect Enum constant declared inwards Enum Type past times passing St Java Enum ValueOf Example - How to move /**
 *
 * Java plan to present How to move valueOf method of Enum
 * to practise enum. This is unproblematic Enum valueOf illustration which
 * teaches how to move valueOf method.
 *
 * @author
 */

public class EnumValueOfExample {

    public static void main(String args[]) {
                 
      //valueOf method returns Enum instance alongside lift matching to String passed to valueOf
      TrafficSignal indicate = TrafficSignal.valueOf("RED");
      System.out.println("name : " + signal.name() + " activity : " + signal.getAction());
   
      //Another Enum valueOf exampel
      indicate = TrafficSignal.valueOf("GREEN");
      System.out.println("name : " + signal.name() + " activity : " + signal.getAction());
   
   
      //valueOf volition throw IllegalArgumentException if nosotros move past times invalid String
      indicate = 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:
lift : RED activity : stop
lift : GREEN activity : start
Exception inwards 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 inwards Java. In curt enum valueOf is a useful method which is past times default available to all enum constants as well as tin dismiss live on used to larn Enum constants from String.

Further Learning
Complete Java Masterclass
Java CyclicBarrier Example - Concurrency tutorial

0 Response to "Java Enum Valueof Illustration - How To Role"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel