• Post Reply Bookmark Topic Watch Topic
  • New Topic

enum program breakdown...  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
simple program using an enum...but i have a few questions



ok so the constructor has to setup parameters in the constants? the (5,2,7) and its private as you cant instantiate an enum right?

i just dont understand the this.days = days; -- i have watched tutorials on the this keyword but dont really understand its use properly.
i know the this this.days = days; is saying use this private final int days; variable right...instead of?
can somebody first help me clear this up...
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:
i know the this this.days = days; is saying use this private final int days; variable right...instead of?


In the context of that line of code, there are two "days" variables that are in scope. There is, as you mentioned, the private instance variable. And there is the parameter, which is a local variable. In that scope, the local variable hides the instance variable.

So, to answer your question, with the "this" reference, the instance variable is chosen, instead of the local variable.

Henry
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Treat enums just like you would a class. An enum type's name should start with a capital letter. The values should be treated as constants so their names should follow the convention for symbolic constants, namely, all caps with "_" as word separators. Their names should be descriptive. Even if this is just an example, the least you can do is choose names that make some sense, otherwise it will just be bad example. If you provide a constructor for an enum type, it should be private. Declaring an enum type constructor as public or protected will result in a compiler error, although I tried to leave at default (package private) and the compiler accepted it, which surprised me.

Here's an example that makes more sense (scroll down to the end to see how the enum types are defined):

And this is the output:

The days of the week are:
SUNDAY (0)
MONDAY (1)
TUESDAY (2)
WEDNESDAY (3)
THURSDAY (4)
FRIDAY (5)
SATURDAY (6)

Days in WORKDAYS include:
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY

Days in WEEKEND include:
SATURDAY
SUNDAY

Days in HUMPDAY include:
WEDNESDAY

SATURDAY is not considered part of WORKDAYS

SATURDAY is considered part of WEEKEND

Today is MONDAY, annoying camel will say:
"Hey, Mike! Mike! Mike! How many days before humpday?"
"2 more days until HUMPDAY!"

Today is TUESDAY, annoying camel will say:
"Hey, Mike! Mike! Mike! How many days before humpday?"
"1 more days until HUMPDAY!"

Today is WEDNESDAY, annoying camel will say:
"Hey, Mike! Mike! Mike! Guess what day it is!"
"IT'S HUMPDAY! IT'S HUMPDAY, Mike!"

Today is THURSDAY, annoying camel will say:
"Hey, Mike! Mike! Mike! How many days before humpday?"
"6 more days until HUMPDAY!"

Today is FRIDAY, annoying camel will say:
"Hey, Mike! Mike! Mike! How many days before humpday?"
"5 more days until HUMPDAY!"
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks appreciate it

will review the code...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!