0

sorry for asking such basic question. I am trying some hands on ENUM. below is my code. I am getting some compilation error . Any idea where is the problem. I want SAMPLEMAIL,BULKUSERS,ALLUSERS should be of integer type.

 public enum EmailSendingOption { SAMPLEMAIL, BULKUSERS, ALLUSERS; private int emailSendingOptionType; private EmailSendingOption(String optionType) { int value = Integer.parseInt(optionType.trim()); emailSendingOptionType = value; } public int getEmailSendingOption() { return emailSendingOptionType; } } 

thanks.

3
  • Post the error please. Commented Feb 13, 2014 at 9:41
  • enum are not integers in Java, they are objects. Commented Feb 13, 2014 at 9:42
  • Constructor EmailSendingOption in enum EmailSendingOption cannot be applied to given types. required: String , found: no arguments Commented Feb 13, 2014 at 9:44

5 Answers 5

3

You have defined a constructor but you haven't supplied arguments for each of your Enums.

Looks like your constructor should take an integer too. Saves having to parse a String each time. It's also safer.

e.g.

SAMPLEMAIL(10), etc.

With your constructor looking like:

private int emailSendingOptionType; private EmailSendingOption(int optionType) { this.emailSendingOptionType = optionType; } 
Sign up to request clarification or add additional context in comments.

2 Comments

+1. A minor correction SAMPLEMAIL(10) should be SAMPLEMAIL("10")
As it stands, yes, but if you look closely, he's taking a String as a parameter in the constructor, then parsing it to an int. Would be best to just change the constructor to take an int.
1

You need to make it like this:

public enum EmailSendingOption { SAMPLEMAIL("1"), BULKUSERS("2"), ALLUSERS("3"); private int emailSendingOptionType; private EmailSendingOption(String optionType) { int value = Integer.parseInt(optionType.trim()); emailSendingOptionType = value; } public int getEmailSendingOption() { return emailSendingOptionType; } } 

Comments

0

enum are not integers in Java, they are objects. There is no sane reason you would pass an integer as a String to a constructor so it can be parsed into an integer. If you want an integer use an integer.

public enum EmailSendingOption { SAMPLEMAIL(1), BULKUSERS(2), ALLUSERS(101); private final int emailSendingOptionType; private EmailSendingOption(int emailSendingOptionType) { this.emailSendingOptionType = emailSendingOptionType; } public int getEmailSendingOption() { return emailSendingOptionType; } } 

Comments

0

Since you are providing a custom constructor to your Enum

EmailSendingOption(String optionType) 

You need to add those parameter for each Enum constant.

Comments

0

Change it to:

public enum EmailSendingOption { SAMPLEMAIL("String"), BULKUSERS("String"), ALLUSERS("String"); ... } 

You defined a constructor as needed a String as argument. SAMPLEMAIL this are a static object of EmailSendingOption.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.