Skip to content

Instantly share code, notes, and snippets.

@k0emt
Created May 14, 2013 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k0emt/5573435 to your computer and use it in GitHub Desktop.
Save k0emt/5573435 to your computer and use it in GitHub Desktop.
Example of working with enums in Java and in particular how to transform them into strings.
public class EnumStringExample {
public static enum DAYS_OF_THE_WEEK {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
public String toString() {
String plainName = name().toString();
String fancy = plainName.charAt(0)
+ plainName.substring(1).toLowerCase();
return fancy;
}
}
public static void main(String[] args) {
System.out.println("implicit: " + DAYS_OF_THE_WEEK.MONDAY);
System.out.println("explicit: " + DAYS_OF_THE_WEEK.TUESDAY.toString());
for (DAYS_OF_THE_WEEK day : DAYS_OF_THE_WEEK.values()) {
System.out.print(day);
switch (day) {
case MONDAY:
case TUESDAY:
case WEDNESDAY:
case THURSDAY:
case FRIDAY:
System.out.println(" weekday");
break;
case SATURDAY:
case SUNDAY:
System.out.println(" weekend! :^D");
break;
default:
System.out.println(" SAY WHAT!");
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment