• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

shifting to the next enum in the list

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a way to create a method within an enum that will shift the value of the enum variable to the next enum value.

What I have so far is a method that lets me change the value manually (so to speak). What I want to accomplish is a method that lets me do something like:

day = test.MON;
day.next();
day now equals test.TUES

The following code works, but only indirectly. What am I missing?



Thanks!
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unless I am missing something, that code is way over the top. Something more on the lines of :-


Note - the index check is not needed if you don't want to rotate round to the start again.
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But instead of just saying: day.next(), try day = day.next() as in the following code. I also updated your next() method to not use ifs and loops, as Mr. Sabre suggested.
 
Daniel Gallant
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First off, thanks for your answers! They're quite helpful. And, yes, I need the values to loop around.

Now, just so I understand it correctly, there is no way to make a self contained method that will change the value of a variable with an enum assigned to it (i.e. It will always be day = day.next(); and never just day.next();), correct?

If that's the case, then that's what I needed to know.

Thanks again for all your help!
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that's correct. Calling a method of an object in Java will never automatically change the value of any variable which happens to contain a reference to the object.
reply
    Bookmark Topic Watch Topic
  • New Topic