Created
January 2, 2013 12:17
-
-
Save dapurv5/4434175 to your computer and use it in GitHub Desktop.
Fun with Java Enums
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
enum Algorithm{ | |
A("a"){ | |
private transient String msg = "I am A returned"; | |
public String f(){ | |
System.out.println("This is algo A"); | |
return msg; | |
} | |
}, | |
B("b"){ | |
private transient String msg = "I am B returned"; | |
public String f(){ | |
System.out.println("This is algo B"); | |
return msg; | |
} | |
}; | |
//Each of the enum constants must implement this abstract method. | |
abstract String f(); | |
String algoName; | |
Algorithm(String algoName){ | |
this.algoName = algoName; | |
} | |
} | |
public class EnumDemo { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
String s = Algorithm.A.f(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment