Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a Java enum start with 1? [closed]

Tags:

java

enums

Taking a look at the source code of java.time.DayOfWeek reveals:

public enum DayOfWeek implements TemporalAccessor, TemporalAdjuster {

    /**
     * The singleton instance for the day-of-week of Monday.
     * This has the numeric value of {@code 1}.
     */
    MONDAY,
    /**
     * The singleton instance for the day-of-week of Tuesday.
     * This has the numeric value of {@code 2}.
     */
    TUESDAY,

    // ..
}

So how can DayOfWeek.MONDAY = 1? I am asking because I use GWT which does not support the new java.time stuff yet (or never will be idk). So what I did I made my own version of DayOfWeek but that one got a public int getValue() { return ordinal() + 1; } which is really annoying since I have to call it everytime.

That is why I am curious why the above version starts with 1. The other thing I'd like to know is why the f this has to start with 1 and not with 0 just like every other enum does. They could have gone for Monday = 0, Tuesday = 1, etc. but no! They instead switched from Sunday = 0, Monday = 1, etc. to that thing up there.


No, this is not an option

public enum MyDayOfWeek {

    DUMMY,

    MONDAY, TUESDAY, // ..
    ;
}

Simply because this would be annoying:

for(MyDayOfWeek day : MyDayOfWeek.value() {
   if(MyDayOfWeek.DUMMY == day) {
      continue;
   }
}

[SUMMARY]

If user-defined enums always start with the first element having the value '0' then how does the first element of the java.time.DayOfWeek enum start with '1', and is there a way to have my enum start with '1' instead of '0'?

like image 607
Stefan Falk Avatar asked Feb 22 '16 18:02

Stefan Falk


People also ask

Do Java enums start at 0 or 1?

enum starts always with 0.

Does an enum start at 0 or 1?

Enum Values The first member of an enum will be 0, and the value of each successive enum member is increased by 1. You can assign different values to enum member. A change in the default value of an enum member will automatically assign incremental values to the other members sequentially.

Can an enum start with a number?

Enum instances must obey the same java language naming restrictions as other identifiers - they can't start with a number.

Can an enum have one value?

With an enum type with only one value, most developers will not expect that (UserIDEnum)42 is used, since it's not a "defined" value of the enum.


2 Answers

To address "how does the enum start with 1"?

It doesn't.

Enums start with 0. They add 1 to the return value:

public int getValue() {
    return ordinal() + 1;
}

Note: In the comments they clearly state:

Do not use ordinal() to obtain the numeric representation of DayOfWeek. Use getValue() instead.

like image 174
MrHappyAsthma Avatar answered Nov 16 '22 03:11

MrHappyAsthma


That is why I am curious why the above version starts with 1.

According to the comments in the code, it's because they wanted to follow the ISO-8601 week date standard:

DayOfWeek is an enum representing the 7 days of the week - Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday.
In addition to the textual enum name, each day-of-week has an int value. The int value follows the ISO-8601 standard, from 1 (Monday) to 7 (Sunday). It is recommended that applications use the enum rather than the int value to ensure code clarity.


The other thing I'd like to know is why the f this has to start with 1 and not with 0 just like every other enum does.

It doesn't.

The enum itself is 0-indexed like any other in Java. It just fiddles with the values to give you 1-7:

public int getValue() {
    return ordinal() + 1;
}

or to turn a 1-7 into the appropriate enum value:

public static DayOfWeek of(int dayOfWeek) {
    if (dayOfWeek < 1 || dayOfWeek > 7) {
        throw new DateTimeException("Invalid value for DayOfWeek: " + dayOfWeek);
    }
    return ENUMS[dayOfWeek - 1];
}
like image 25
azurefrog Avatar answered Nov 16 '22 04:11

azurefrog