public enum EnumEx {
MALE("male"),FEMALE("");
private String value;
EnumEx(String value){
this.value = value;
};
public String value() {
return value;
}
}
public class TestEx {
public static void main(String[] args) {
System.out.println(EnumEx.MALE.value()); // custom method value()
System.out.println(EnumEx.MALE.name()); // predefined method name()
}
}
output:
male
MALE
=============================================
Can avoid using:
public interface Constants{
String MALE = "male";
.....
}
Enum by default is Singleton and it's threadsafe.
No comments:
Post a Comment