Salam
Salam

Reputation: 33

Define enum with variable with List<String> type

I'm working on spring boot application and I have a case where I want to define enum with List as type. But I got a syntax error to pass list. Do we have workaround to solve this syntax error?

My code

EMAIL("001", "email", "Send To Email"),
    SMS("002", "slack", "Send To SMS"),
    EMAIL_SMS("003", "email", "Send to SMS and Email");


    private String code;
    private String description;
    private List<String> dest = new ArrayList<>();

    NotificationCenterCodeEnum(String  code, List<String> dest, String description) {
        this.code = code;
        this.dest=dest;
        this.description = description;
    }

Upvotes: 1

Views: 52

Answers (2)

Mouad EL Fakir
Mouad EL Fakir

Reputation: 3749

Try this :

enum Notification {

    EMAIL("code 1", "description 1", "email-2", "email-2"),
    SMS("code 2", "description 2", "num-1", "num-2", "num-3");

    Notification(String code, String description, String... dest) {
        this.code = code;
        this.description = description;
        this.dest = dest;
    }

    private String code;
    private String description;
    private String[] dest;

    // getters ...
}

Use :

public class Hello {

    public static void main(String[] args) {

        String[] emails = Notification.EMAIL.getDest();
        String[] nums = Notification.SMS.getDest();

    }

}

Upvotes: 2

Simon Martinelli
Simon Martinelli

Reputation: 36163

You are not passing the second argument as list:

EMAIL("Code-001", "email", "Send To Email"),

Should be

EMAIL("Code-001", Arrays.asList("email"), "Send To Email"),

Upvotes: 2

Related Questions