• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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!
 
Sheriff
Posts: 27460
88
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.
 
Goodbye moon men. Hello tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic