Skip to content

Instantly share code, notes, and snippets.

@dapurv5
Created January 2, 2013 12:17
Show Gist options
  • Save dapurv5/4434175 to your computer and use it in GitHub Desktop.
Save dapurv5/4434175 to your computer and use it in GitHub Desktop.
Fun with Java Enums
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