4

I am trying to declare this enum:

public enum Month {
    1, 2, 3, 4, 5 , 6, 7, 8, 9, 10, 11, 12;
}

But when I try to compile it doesn't work. Is this because the constants are integers?

4 Answers 4

5

Yes - the values of an enum have to be valid identifiers. They're basically static fields after all - you're effectively trying to declare:

public static Month 1 = new Month();

which obviously isn't valid.

See the Java Language Specification section 8.9 for details, but in particular this production:

EnumConstant:
Annotations Identifier Argumentsopt ClassBodyopt

Sign up to request clarification or add additional context in comments.

2 Comments

So is there any way I can use integers? Can I declare them as strings and parse them into integers later on?
@James: It's not clear what you mean. You're declaring members, and those members have to have valid identifiers. You're not going to be able to refer to Month.1, no.
2
public enum Month {
  JANUARY(1),
  FEBRUARY(2),
  ...
  DECEMBER(12);

  private int number;

  private Month(int number) {
    this.number = number;
  }

  public int getNumber() {
    return number;
  }
}

Comments

1

As @Jon Skeet said, you are not going to be able to use integers as identifiers in your code. The closest thing that you can do is associate a integer value to your constants:

public enum Month {
   ONE(1),
   TWO(2);

   private final int number;

   private Month(int number) {
     this.number = number;
   }

   public int getNumber() { return number; }
}

so that you can do something like this:

Month.ONE.getNumber()

not sure if this suits your needs though.

4 Comments

Ahh that doesn't really suit what I need, but I now understand enums better. My problem is, is that I have to create my own calendar class (I know one exists, but we need to create our own). And we have been told to use enums. I'm honestly not sure what I should use enumerated types for. Could you give me some examples as to where these would be used?
enums can be used to create a data type which only a small set of possible values. Weekdays and months are actually the most popular examples for enum types. I don't know why you think that enums are unsuited for your calendar application.
I don't think they're unsuited for my class, I'm just not familiar with them, so I'm trying to understand where I should use them. So if I declared an enum type of the weekdays, what could I then do with that?
First of all: enums can do everything other classes can do. You can declare members and method just for any other class. The only exception is that you can not create new instances of your enum class - Java does that for you. A Weekday enum could hold additional data like name (Monday), shortName (Mon) or a boolean flag isWeekend. Since the enum instances are public static final you can access that data from everywhere: Weekday.MONDAY.isWeekend(). When you later use Weekday in a Day class, you could do thinks like if(day.getWeekday().isWeekend()){...}
0

You use this horrible work around.

public enum Month {
    _1, _2, _3, _4, _5 , _6, _7, _8, _9, _10, _11, _12;
}

However another approach is to use name and ordinal() for numbers.

public enum Month {
    None, 
    January, February, March, April, May, June, 
    July, August, September, October, November, December
}

Month month = Month.January;
int num = month.ordinal(); // == 1
Month month2 = Month.values()[num]; // == January.

4 Comments

using ordinal is usually discouraged (by Effective Java among others). If you want indexes, it is usually a good idea to assign them explicitly.
It would helpful to say why it is discouraged. Otherwise it sounds like more work to do something Java does for you. I tend to subscribe to the YAGNI principle.
this is what Effective Java says about it: "it is a maintenance nightmare. If the constants are reordered, the method will break. If you want to add a second enum constant associated with an int value that you've already used, you're out of luck. ... . Also you can't add a constant for an int value without adding constants for all intervening int values." These points don't really apply to this case, but I guess it's better for consistency if you use all your enums in the same way
Having-to-have-a-dummy-constant applies here. ;) It is true this is not as flexible maintenance wise. To give you an idea how often Months change Sept- means 7, Oct- means 8, Non- means 9 and Dece- means 10. The months were last re-ordered in 1583, March used to be the first month, but the names were not changed. It depends on the project as to whether following a standard or c2.com/xp/YouArentGonnaNeedIt.html is more important.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.