Java Enum Alongside Constructor Example
Java Enum alongside Constructor
Many Java developers don't know that Java Enum tin receive got a constructor to function yesteryear information spell creating Enum constants. This characteristic allows you lot to associate related information together. One representative of passing arguments to enum Constructor is our TrafficLight Enum where nosotros function yesteryear activity to each Enum representative e.g. GREEN is associate alongside go, RED is associated alongside stop as well as ORANGE is associated alongside the slow down. This is actually useful because it provides to a greater extent than context as well as pregnant to your code. If you lot want, you lot tin every bit good supply i or to a greater extent than constructor to your Enum every bit it every bit good supports constructor overloading like normal Java classes. This is really dissimilar from enum you lot receive got seen inward C or C++, which is simply a collection of fixed matter without whatever OOP power.
Just recollect that constructor inward enums an exclusively survive either somebody or bundle score it can't survive world or protected thus access modifier public as well as protected are non allowed to Enum constructor, it volition upshot inward compile fourth dimension error.
By the way, this is non our kickoff tutorial on Java Enum where nosotros receive got explained a fundamental feature, nosotros receive got every bit good ready covered simply about of import features on Enum inward our previous examples similar Java Enum Switch Example explains that you lot tin role enum constants within switch block.
Similarly, Java Enum valueOf Example and Enum to String Example inward explains that how you lot tin acquire the String representation of Enum. These brusque tutorials are a expert agency to larn simply about useful features of enum inward Java.
Just recollect that constructor inward enums an exclusively survive either somebody or bundle score it can't survive world or protected thus access modifier public as well as protected are non allowed to Enum constructor, it volition upshot inward compile fourth dimension error.
By the way, this is non our kickoff tutorial on Java Enum where nosotros receive got explained a fundamental feature, nosotros receive got every bit good ready covered simply about of import features on Enum inward our previous examples similar Java Enum Switch Example explains that you lot tin role enum constants within switch block.
Similarly, Java Enum valueOf Example and Enum to String Example inward explains that how you lot tin acquire the String representation of Enum. These brusque tutorials are a expert agency to larn simply about useful features of enum inward Java.
Java Enum alongside Constructor Example
String declaration which is saved to activity land which is afterwards accessed yesteryear getter method getAction().
As I explained, nosotros receive got an Enum TrafficSignal, which receive got iii enum constants, RET, GREEN, as well as ORANGE, as well as nosotros receive got associated, wait, popular off as well as deadening downwardly alongside them yesteryear passing values into the constructor.
As I explained, nosotros receive got an Enum TrafficSignal, which receive got iii enum constants, RET, GREEN, as well as ORANGE, as well as nosotros receive got associated, wait, popular off as well as deadening downwardly alongside them yesteryear passing values into the constructor.
/**
* Java enum alongside constructor for example.
* Constructor bring i String declaration action
*/
public enum TrafficSignal{
//this volition telephone phone enum constructor alongside i String argument
RED("wait"), GREEN("go"), ORANGE("slow down");
private String action;
public String getAction(){
return this.action;
}
// enum constructor - tin non survive world or protected
TrafficSignal(String action){
this.action = action;
}
}
/**
*
* Java Enum representative alongside constructor.
* Java Enum tin receive got constructor but it tin not
* survive world or protected
*
* @author http://java67.com
*/
public class EnumConstructorExample{
public static void main(String args[]) {
//let's impress advert of each enum as well as in that place action
// - Enum values() examples
TrafficSignal[] signals = TrafficSignal.values();
for(TrafficSignal quest : signals){
//Java advert representative - Java getter method example
System.out.println("name : "
+ signal.name()
+ " action: "
+ signal.getAction());
}
}
}
* Java enum alongside constructor for example.
* Constructor bring i String declaration action
*/
public enum TrafficSignal{
//this volition telephone phone enum constructor alongside i String argument
RED("wait"), GREEN("go"), ORANGE("slow down");
private String action;
public String getAction(){
return this.action;
}
// enum constructor - tin non survive world or protected
TrafficSignal(String action){
this.action = action;
}
}
/**
*
* Java Enum representative alongside constructor.
* Java Enum tin receive got constructor but it tin not
* survive world or protected
*
* @author http://java67.com
*/
public class EnumConstructorExample{
public static void main(String args[]) {
//let's impress advert of each enum as well as in that place action
// - Enum values() examples
TrafficSignal[] signals = TrafficSignal.values();
for(TrafficSignal quest : signals){
//Java advert representative - Java getter method example
System.out.println("name : "
+ signal.name()
+ " action: "
+ signal.getAction());
}
}
}
This was our Java Enum representative alongside Constructor. Now, you lot know that Enum tin have a constructor inward Java which tin survive used to function yesteryear information to Enum constants, simply similar nosotros passed activity here. Though Enum constructor cannot survive protected or public, it tin either receive got somebody or default modifier only.
Further Learning
Complete Java Masterclass
In-depth Guide of Enum inward Java
10 Things Java Programmers Should Learn inward 2018
How to acquire a improve Java developer
Thanks for reading this article so far. If you lot similar this brusque tutorial as well as then delight portion alongside your friends as well as colleagues. If you lot receive got whatever questions or feedback as well as then delight drib a note.
0 Response to "Java Enum Alongside Constructor Example"
Post a Comment