I have a enum and every enum must have specific fields, such as price, which will not be modified, and I want to create a class to get this fields, what can I do in this situation? How can I call the class using the enums or what can I do?
CodePudding user response:
You can try this :
public enum SalaryHeadMasterEnum {
BASIC_PAY("basic pay"),
MEDICAL_ALLOWANCE("Medical Allowance");
private String name;
private SalaryHeadMasterEnum(String stringVal) {
name=stringVal;
}
public String toString(){
return name;
}
public static String getEnumByString(String code){
for(SalaryHeadMasterEnum e : SalaryHeadMasterEnum.values()){
if(e.name.equals(code)) return e.name();
}
return null;
}
}
Now you can use below code to retrieve the Enum by Value
SalaryHeadMasterEnum.getEnumByString("Basic Pay")
Use Below code to get ENUM as String
SalaryHeadMasterEnum.BASIC_PAY.name()
Use below code to get string Value for enum
SalaryHeadMasterEnum.BASIC_PAY.toString()
CodePudding user response:
An enum is a special type of class that represents a group of fianl values. if you want to get the field then you make instance like
Price myVar = Price.Low;
System.out.println(myVar);
