Below is the enum declaration
public enum EnumConstant {
STRING_1("ONE"), STRING_2("TWO");
private final String stringValue;
private EnumConstant(final String s) {
stringValue = s;
}
public String toString() {
return stringValue;
}
}
Below is the test class to know its functions
public class EnumStringTest {
public static void main(String... arg){
System.out.println("Enum value :"+EnumConstant.STRING_1);
System.out.println("Enum value :"+EnumConstant.STRING_1.toString());
System.out.println("Enum name : "+EnumConstant.STRING_1.name());
System.out.println("Position of Enum constant : "+EnumConstant.STRING_1.ordinal());
}
}
Result :
Enum value :ONE
Enum value :ONE
Enum name : STRING_1
Position of Enum constant : 0
Other way to use is as follows
public enum EnumVariable {
STRING_1{
public String toString(){
return "ONE";
}
},
STRING_2{
public String toString(){
return "TWO";
}
}
}
No comments:
Post a Comment