• Post Reply Bookmark Topic Watch Topic
  • New Topic

shifting to the next enum in the list  RSS feed

 
Daniel Gallant
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!
 
James Sabre
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.
 
Tom Reilly
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!
 
Paul Clapham
Sheriff
Posts: 22525
43
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!