Converting Enum into String together with parsing String to Enum inward Java is becoming a mutual trace alongside growing exercise of Enum. Enum is really versatile inward Java together with preferred the choice to correspond bounded information together with since is almost used everywhere to comport literal value it's of import to know how to convert Enum to String in Java. In this article, nosotros volition come across both start converting Strings to Enum inward Java together with thus Change an Enum to String inward Java alongside Example. I thought about this Enum tutorial when I wrote 10 Examples of Enum inward Java. I missed String to Enum conversion together with 1 of reader pointed out that. So hither nosotros cause got now.
Enum to String to Enum inward Java
This article is inward continuation of other conversion-related posts e.g. how to convert Date to String inward Java together with How to Convert String to Integer inward Java. As these are mutual needs together with having the best means to produce things inward hear saves lot of fourth dimension spell coding.
Convert Enum to String inward Java Example
Enum classes past times default render valueOf (String value) method which takes a String parameter together with converts it into an enum. String elevate should correspond with text used to declare Enum inward Java file. Here is a complete code event of String to Enum inward Java
Code Example String to Enum:
/**
* Java Program to parse String to Enum inward Java alongside examples.
*/
public class EnumTest {
private enum LOAN {
HOME_LOAN {
@Override
public String toString() {
return "Always expect for cheaper Home loan";
}
},
AUTO_LOAN {
@Override
public String toString() {
return "Cheaper Auto Loan is better";
}
},
PEROSNAL_LOAN{
@Override
public String toString() {
return "Personal loan is non cheaper whatever more";
}
}
}
public static void main(String[] args) {
// Exmaple of Converting String to Enum inward Java
LOAN homeLoan = LOAN.valueOf("HOME_LOAN");
System.out.println(homeLoan);
LOAN autoLoan = LOAN.valueOf("AUTO_LOAN");
System.out.println(autoLoan);
LOAN personalLoan = LOAN.valueOf("PEROSNAL_LOAN");
System.out.println(personalLoan);
}
}
Output:
Always expect for cheaper Home loan
Cheaper Auto Loan is better
Personal loan is non cheaper anymore
Convert Enum to String inward Java Example
Now let's produce reverse convert an Enum into String inward Java, in that place are multiple ways to produce it 1 means is to render exact same String used to declare Enum from toString() method of Enum, otherwise if you lot are using toString() method for some other role thus you lot tin forcefulness out exercise default static name() method to convert an Enum into String. Java past times default adds name() method into every Enum together with it returns just same text which is used to declare enum inward Java file.
Code Example Enum to String
public static void main(String[] args) {
// Java event to convert Enum to String inward Java
String homeLoan = LOAN.HOME_LOAN.name();
System.out.println(homeLoan);
String autoLoan = LOAN.AUTO_LOAN.name();
System.out.println(autoLoan);
String personalLoan = LOAN.PERSONAL_LOAN.name();
System.out.println(personalLoan);
}
Output:
HOME_LOAN
AUTO_LOAN
PERSONAL_LOAN
That’s all on How to parse String to Enum inward Java together with convert Enum to String object . This tip volition assistance you lot to speedily convert your information betwixt ii most versatile types Enum together with String inward Java. If you lot know whatever other means to alter String to Enum inward coffee thus delight allow us know.
Further Learning
Complete Java Masterclass
How to convert String to engagement inward java