• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

enum program breakdown...

 
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 16669
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks appreciate it

will review the code...
reply
    Bookmark Topic Watch Topic
  • New Topic