Last active
April 27, 2018 06:55
-
-
Save talhahasanzia/70a47889bf4eb942b861f0a410fc8434 to your computer and use it in GitHub Desktop.
Enums usage. From Java 7, Strings are also supported.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// declaration | |
public enum Season { | |
SPRING("Spring"), SUMMER("Summer"), FALL("Fall"), WINTER("Winter"); | |
private String name; | |
Season(String name) { | |
this.name = name; | |
} | |
public String getName() { | |
return name; | |
} | |
} | |
// sample usage | |
public void printSeason(Season season) { | |
System.out.println("It is " + season.getName() + " now!"); | |
} | |
// overriding toString() example | |
public enum Currency { | |
........ | |
@Override | |
public String toString() { | |
switch (this) { | |
case PENNY: System.out.println("Penny: " + value); break; | |
case NICKLE: System.out.println("Nickle: " + value); break; | |
case DIME: System.out.println("Dime: " + value); break; | |
case QUARTER: System.out.println("Quarter: " + value); } | |
return super.toString(); } | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment