Java Enum inward Switch Case Statement
Yesterday, about 1 enquire me Can nosotros usage Java Enum inward Switch case ? Obviously, he was learning Enum too non aware that How powerful Enum inward Java is. Yes, You tin usage Enum inward Switch instance contestation inward Java similar int primitive. If yous are familiar amongst enum int pattern, where integers correspond enum values prior to Java five thence yous already knows how to use Switch instance amongst Enum. Using Java Enum inward Switch instance is pretty straightforward, Just usage Enum reference variable inward Switch too Enum constants or instances inward CASE statement. In this Java tutorial nosotros volition encounter 1 illustration of How to usage Enum inward Switch contestation inward Java. By the means Enum is characteristic rich inward Java, Enum tin implement interface inward Java, Enum tin override method inward Java, Enum tin stimulate got constructor inward Java too Enum is total functional Type similar bird or interface. Programmers usage Enum amongst dissimilar ways. One of the best illustration of Enum inward Java is supervene upon enum int pattern too enum String pattern. You tin too use Enum to write Thread-safe Singleton inward Java.
Enum inward Switch instance - Java
In this illustration of using Enum inward Switch case, nosotros stimulate got created a Days of Week Enum which correspond all days e.g. Monday, Tuesday etc. Then nosotros acquire all Enum instances inward an array using Enum.values() method too yesteryear using advanced for loop of Java 5, nosotros loop through each of that Enum instance. By the way, yous tin too usage String inward Switch instance from Java7 onwards. Similarly, yous tin too convert Enum to String inward Java.
In the trunk of for loop, nosotros stimulate got created a switch instance contestation using Enum inward Java. Which switch on electrical current Enum instance too every CASE contestation is private Enum instance e.g. DAY.MONDAY or DAY.TUESDAY. Since yous stimulate got declared Enum inward the same bird yous tin usage their instance without calling them amongst bird lift e.g. MONDAY . Here is consummate code illustration of using Enum inward Switch instance inward Java :
/**
*
* Java computer program to demonstrate how to usage Enum inward Switch instance statement.
* Enum tin hold upwardly used inward switch block similar to primitive int or enum int pattern.
* Enum tin too hold upwardly used inward CASE contestation to stimulate got appropriate activity based upon
* Enum instance.
*
* @author Javin
*/
public class EnumInSwitch {
public enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
}
public static void main(String args[]) {
Day[] daysOfWeek = Day.values();
for (Day today : daysOfWeek) {
//Using Enum inward Switch instance statement
switch (today) {
case MONDAY:
System.err.println("Today is Mon larn illustration of How to usage Java Enum inward Switch");
break;
case TUESDAY:
System.err.println("Tuesday, apply Enum inward Switch only link primitive int");
break;
case WEDNESDAY:
System.err.println("Wednesday, I confirm Java Enum tin hold upwardly used inward Switch case");
break;
case THURSDAY:
System.err.println("Thursday, Java Enum values() method furnish all enum inward an array");
break;
case FRIDAY:
System.err.println("Friday, Enum tin too hold upwardly used inward instance statement");
break;
case SATURDAY:
System.err.println("Saturday, Enum inward Java are compile fourth dimension constant");
break;
case SUNDAY:
System.err.println("Sunday, Using Enum inward Switch is real easy");
break;
}
}
}
}
Output:
Today is Mon larn illustration of using Java Enum inward Switch
Tuesday, apply Enum inward Switch only link primitive int
Wednesday, I confirm Java Enum tin hold upwardly used inward Switch case
Thursday, Java Enum values() method return all enum inward an array
Friday, Enum tin too hold upwardly used inward case statement
Saturday, Enum inward Java are compile fourth dimension constant
Sunday, Using Enum inward Switch is real easy
*
* Java computer program to demonstrate how to usage Enum inward Switch instance statement.
* Enum tin hold upwardly used inward switch block similar to primitive int or enum int pattern.
* Enum tin too hold upwardly used inward CASE contestation to stimulate got appropriate activity based upon
* Enum instance.
*
* @author Javin
*/
public class EnumInSwitch {
public enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
}
public static void main(String args[]) {
Day[] daysOfWeek = Day.values();
for (Day today : daysOfWeek) {
//Using Enum inward Switch instance statement
switch (today) {
case MONDAY:
System.err.println("Today is Mon larn illustration of How to usage Java Enum inward Switch");
break;
case TUESDAY:
System.err.println("Tuesday, apply Enum inward Switch only link primitive int");
break;
case WEDNESDAY:
System.err.println("Wednesday, I confirm Java Enum tin hold upwardly used inward Switch case");
break;
case THURSDAY:
System.err.println("Thursday, Java Enum values() method furnish all enum inward an array");
break;
case FRIDAY:
System.err.println("Friday, Enum tin too hold upwardly used inward instance statement");
break;
case SATURDAY:
System.err.println("Saturday, Enum inward Java are compile fourth dimension constant");
break;
case SUNDAY:
System.err.println("Sunday, Using Enum inward Switch is real easy");
break;
}
}
}
}
Output:
Today is Mon larn illustration of using Java Enum inward Switch
Tuesday, apply Enum inward Switch only link primitive int
Wednesday, I confirm Java Enum tin hold upwardly used inward Switch case
Thursday, Java Enum values() method return all enum inward an array
Friday, Enum tin too hold upwardly used inward case statement
Saturday, Enum inward Java are compile fourth dimension constant
Sunday, Using Enum inward Switch is real easy
That's all on How to usage Enum inward Switch instance inward Java. Its indeed real slowly too afterwards trying it 1 fourth dimension , yous volition hold upwardly familiar amongst that. Just acquire yourself familiar amongst Enum API too about useful methods similar name(), cardinal(), values() etc, which volition assistance yous to acquire around of Java Enum apart from only using Enum inward Switch case.
Further Learning
Complete Java Masterclass
How to practise Enum from String inward Java
No comments:
Post a Comment