Saturday, May 4, 2024
HomeJavaThe best way to use Java Enum in Change Case Assertion -...

The best way to use Java Enum in Change Case Assertion – Exampel Tutorial


Disclosure: This text could comprise affiliate hyperlinks. Whenever you buy, we could earn a fee.

Java Enum in Change Case Assertion

Yesterday, somebody ask me Can we use Java Enum in Change case? Clearly, he was studying Enum and never conscious that How highly effective Enum in Java is. Sure, You should use Enum in Change case assertion in Java like int primitive. In case you are aware of enum int sample, the place integers characterize enum values previous to Java 5 then you definitely already is aware of how one can  use the Change case with Enum. Utilizing Java Enum within the Change case is fairly easy, Simply use Enum reference variable in Change and Enum constants or cases in CASE assertion. On this Java tutorial we’ll see one instance of The best way to use Enum in Change assertion in Java

Enum in Change case – Java

On this instance of utilizing Enum in Change case, now we have created a Days of Week Enum which represents all days e.g. Monday, Tuesday and so forth. Then we get all Enum cases in an array utilizing Enum.values() technique and by utilizing superior for loop of Java 5, we loop by way of every of that Enum occasion. 

Within the physique of for loop, now we have created a swap case assertion utilizing Enum in Java. Which swap on present Enum occasion and each CASE assertion is particular person Enum occasion e.g. DAY.MONDAY or DAY.TUESDAY

Since you will have declared Enum in the identical class you should utilize their occasion with out calling them with class title e.g. MONDAY. Here’s a full code instance of utilizing Enum in Change case in Java :

How to use Enum in Switch case in Java with Example/**
 *
 * Java program to show how one can use Enum in Change case assertion.
 * Enum can be utilized in swap block much like primitive int or enum int sample.
 * Enum may also be utilized in CASE assertion to take acceptable motion based mostly upon
 * Enum occasion.
 *
 * @writer Javin
 */

public class EnumInSwitch {

    public enum Day {

        MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
    }

    public static void predominant(String args[]) {

        Day[] daysOfWeek = Day.values();

        for (Day in the present day : daysOfWeek) {

            //Utilizing Enum in Change case assertion

            swap (in the present day) {
                case MONDAY:
                    System.err.println(“At present is Monday study instance of The best way to use Java Enum in Change”);
                    break;
                case TUESDAY:
                    System.err.println(“Tuesday, apply Enum in Change simply hyperlink primitive int”);
                    break;
                case WEDNESDAY:
                    System.err.println(“Wednesday, I verify Java Enum can be utilized in Change case”);
                    break;
                case THURSDAY:
                    System.err.println(“Thursday, Java Enum values() technique return all enum in an array”);
                    break;
                case FRIDAY:
                    System.err.println(“Friday, Enum may also be utilized in case assertion”);
                    break;
                case SATURDAY:
                    System.err.println(“Saturday, Enum in Java are compile time fixed”);
                    break;
                case SUNDAY:
                    System.err.println(“Sunday, Utilizing Enum in Change may be very straightforward”);
                    break;

            }
        }
    }
}

Output:
At present is Monday study instance of utilizing Java Enum in Change
Tuesday, apply Enum in Change simply hyperlink primitive int
Wednesday, I verify Java Enum can be utilized in Change case
Thursday, Java Enum values() technique return all enum in an array
Friday, Enum may also be utilized in case assertion
Saturday, Enum in Java are compile time fixed
Sunday, Utilizing Enum in Change may be very straightforward

That is all on The best way to use Enum in Change case in Java. Its certainly very straightforward and after attempting it one time , you may be aware of that. Simply get your self aware of Enum API and a few helpful strategies just like the title(), cardinal(), values() and so forth, which can make it easier to to get most of Java Enum other than simply utilizing Enum in Change case.

Associated Java Enum Tutorials for additional studying :

  1. High 15 Java Enum Interview Questions with Solutions (see right here)
  2. Distinction between RegularEnumSet and JumboEnumSet in Java (learn right here)
  3. String to Enum in Java with Instance (examine right here)
  4. Can we use Enum in Change Assertion in Java (Sure)
  5. The best way to use valueOf technique of Enum (examine right here)
  6. What Each Java Programmer Ought to find out about Enum (click on right here)
  7. 10 factors about Enum in Java (see right here)
  8. The best way to loop over Enum constants in Java (instance)
  9. Learn to use Enum in Java with a Easy Instance (examine right here)
  10. Java tip to transform  Enum to String in Java (see tip)
  11. Can Enum have Constructor in Java (study right here)
  12. 10 Programs to study Java for Learners (programs)

Thanks for studying this text thus far. In the event you discover this text helpful then please share it on Fb, Twitter, and LinkedIn. 

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments