M
M
Melmen2016-10-08 22:33:22
Java
Melmen, 2016-10-08 22:33:22

How to pass arguments to an enum constructor?

Hey! /o
There is an enum list , I specify certain values ​​in it. You need to pass these values ​​from the list to the instance constructor. How can I get variables from enum and use them?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
parkito, 2016-10-08
@parkito

public class EnumTest {
    Day day;
 
    public EnumTest(Day day) {
        this.day = day;
    }
 
    public void tellItLikeItIs() {
        switch (day) {
            case MONDAY:
                System.out.println("Mondays are bad.");
                break;
 
            case FRIDAY:
                System.out.println("Fridays are better.");
                break;
 
            case SATURDAY: case SUNDAY:
                System.out.println("Weekends are best.");
                break;
 
            default:
                System.out.println("Midweek days are so-so.");
                break;
        }
    }
 
    public static void main(String[] args) {
        EnumTest firstDay = new EnumTest(Day.MONDAY);
        firstDay.tellItLikeItIs();
        EnumTest thirdDay = new EnumTest(Day.WEDNESDAY);
        thirdDay.tellItLikeItIs();
        EnumTest fifthDay = new EnumTest(Day.FRIDAY);
        fifthDay.tellItLikeItIs();
        EnumTest sixthDay = new EnumTest(Day.SATURDAY);
        sixthDay.tellItLikeItIs();
        EnumTest seventhDay = new EnumTest(Day.SUNDAY);
        seventhDay.tellItLikeItIs();
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question