• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

shifting to the next enum in the list  RSS feed

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • 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
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 23646
49
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!