I recently have looked at a tutorial on enums. First and foremost, the syntax goes way over my head. Past that, I do not seem to understand the topic and its specific uses either. The tutorial was vague and the commentator just seemed to write code and talk, not necisarrily explaining the code she was using. Please do not just down vote my question because you personally think it is an easy topic. I have already looked at tutorials and found them confusing. If you feel you can not answer my question a link to a website or a you tube video would be appreciated as well. Happy Coding!
Asked
Active
Viewed 317 times
0
-
So what problem do you want to serve with enums? – Drux Jun 28 '14 at 16:13
-
Have you looked at [this](http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html)? – ChiefTwoPencils Jun 28 '14 at 16:14
-
Please read the help center. Half your question text is noise. – Sotirios Delimanolis Jun 28 '14 at 16:16
-
Look for Joshua Bloch, Effective Java, 2nd Edition. Chapter 6, Item 30 covers the basics of Enums and the whole book is well worth reading. The PDF can be downloaded for free. – rossum Jun 28 '14 at 16:31
-
Wow they banned my acount from asking questions. Their help center states you connot get banned for just a few downvoted posts. This is not true ): – Jun 28 '14 at 16:41
-
I don't see your account banned. – Sotirios Delimanolis Jun 28 '14 at 16:47
-
@SotiriosDelimanolis That information is not visible to non-moderators. – C. K. Young Jun 28 '14 at 16:50
-
@ChrisJester-Young What does the ban consist of if they can post? – Sotirios Delimanolis Jun 28 '14 at 16:50
-
@SotiriosDelimanolis What the OP means is that they aren't able to post any new questions subsequent to this one, because they've become question-banned. I can believe that. – C. K. Young Jun 28 '14 at 16:51
-
@ChrisJester-Young I see. – Sotirios Delimanolis Jun 28 '14 at 16:52
-
@user3781061 Ouch, I see you have a couple of negatively-scored questions, plus a zero-scored one here, and more importantly, no positively-scored ones. This is indeed bad news for your ability to continue posting on this site. (Note that I can't see any deleted questions you may have, but those will penalise you too. So the picture may be even worse than I can see.) – C. K. Young Jun 28 '14 at 17:00
-
@user3781061 It is very, very important that most of your questions have a positive score, if you want to avoid being question-banned. I've written some about this topic here: https://www.quora.com/How-do-I-avoid-getting-Sorry-we-are-no-longer-accepting-questions-from-this-account-on-StackOverflow/answer/Chris-Jester-Young – C. K. Young Jun 28 '14 at 17:02
1 Answers
1
Enums are for concepts that have a fixed set of values. A good example of enum is the months in a year. In the Gregorian calendar, there are twelve months, and you can create a month enum thusly:
enum Month {
JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE,
JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER
}
You can imbue individual members with specific behaviour, for example, we can give a numberOfDays
property:
enum Month {
JANUARY(31), FEBRUARY(28), MARCH(31), APRIL(30), MAY(31), JUNE(30),
JULY(31), AUGUST(31), SEPTEMBER(30), OCTOBER(31), NOVEMBER(30), DECEMBER(31);
private final int numberOfDays;
Month(int numberOfDays) {
this.numberOfDays = numberOfDays;
}
public int getNumberOfDays() {
return numberOfDays;
}
}
However, you'll notice that February doesn't have 28 day every year. Instead, you'll need to give it even more special behaviour. So, then, you create a class body for FEBRUARY
to do so:
enum Month {
JANUARY(31),
FEBRUARY(28) {
@Override
public int getNumberOfDays(int year) {
return isLeapYear(year) ? 29 : 28;
}
private boolean isLeapYear(int year) {
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
},
MARCH(31), APRIL(30), MAY(31), JUNE(30), JULY(31),
AUGUST(31), SEPTEMBER(30), OCTOBER(31), NOVEMBER(30), DECEMBER(31);
private final int numberOfDays;
Month(int numberOfDays) {
this.numberOfDays = numberOfDays;
}
public int getNumberOfDays(int year) {
return numberOfDays;
}
}

C. K. Young
- 219,335
- 46
- 382
- 435