This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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: 24088
54
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.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!