We all know that how powerful enumeration type inwards Java is, in addition to 1 of the principal forcefulness of enum is that they tin implement an interface, they tin accept an instance variable in addition to y'all tin likewise override whatsoever method within enum instance. In Java programs, nosotros ofttimes demand to convert Enum to String type, sometimes but to impress values inwards log file in addition to other fourth dimension for storing log into database. By default, when y'all impress an enum constant, it impress its literal value e.g. if get upward of enum instance is RED, in addition to then it volition impress RED. This is likewise the value which is returned past times name() method of java.lang.Enum class. But, at that spot are situations when nosotros desire a custom String value for enum constant. For example, I desire to impress Red instead of RED when I convert Enum to String. How exercise y'all exercise that? Well, at that spot are 2 ways y'all tin accomplish this, commencement past times overriding toString() method for each enum constant in addition to instant past times using an instance variable to concord custom String value. You tin render custom value spell creating Enum constants in addition to after y'all tin telephone phone that method which returns custom String value. In this article, nosotros volition run into instance of these 2 ways.
In our example, nosotros accept an enumeration type called Color, it has 3 enum constants RED, GREEN, and BLUE. By default when y'all travel past times whatsoever of these enumeration constants to System.out.println() method, it volition telephone phone toString() method of java.lang.Object. Since every enum past times default extends java.lang.Enum, which overrides toString() method.
As shown inwards next code snippet, this overridden version returns the ame, which is the literal value of enumeration constant.
You tin run into the overnice advice given their inwards cast of Javadoc comments. It clearly suggests that y'all should override toString() method inwards enum if y'all demand to a greater extent than "programmer-friendly" string. Another agency to render a custom String is to shop that value on a fellow member value in addition to render it past times creating a custom method, scream back y'all cannot exercise toString() but tin for certain exercise getCustomString(). Now y'all may think what is the divergence betwixt these 2 approach? Well, at that spot is a pregnant difference. First agency is genuinely the correct agency to render custom String value because toString() is criterion method for such task. Many framework in addition to other method e.g. print() in addition to println() method from System class, calls this method when y'all travel past times an enum. You don't acquire this awesome characteristic amongst whatsoever other method. Actually instant agency is extension of associating a custom value amongst enum, which could endure integer, String or anything else. In our program, ColorWithToString overrides toString() to render custom String value and ColorWithSpecificString uses an instance variable to concord a specific String value. here is amount code instance :
That's all nigh how to impress custom string value of enum inwards Java. As suggested past times Oracle Java developer's equally well, the correct agency to render an String other than literal value is past times overriding toString() method. This method is meant to render a String value in addition to used past times many developers to convert an Enum to String equally well. Second method likewise highlight, how y'all tin purpose enum equally amount blown class, defining instance variable, creating methods in addition to writing code there. You tin purpose this technique to associate anything amongst your enum constant inwards Java.
Further Learning
Data Structures in addition to Algorithms: Deep Dive Using Java
here)Difference betwixt RegularEnumSet in addition to JumboEnumSet inwards Java (read here) What Every Java Programmer Should know nigh Enum (click here) 10 points nigh Enum inwards Java (see here) Learn how to purpose Enum inwards Java amongst a Simple Example (check here) Can Enum accept Constructor inwards Java (learn here) Can nosotros purpose Enum inwards Switch Statement inwards Java (Yes) How to purpose valueOf method of Enum (check here) How to loop over Enum constants inwards Java (example) Java tip to convert Enum to String inwards Java (see tip) String to Enum inwards Java amongst Example (check here)
Java Program to impress custom String values to Enum inwards Java
As I said at that spot are 2 ways y'all tin associate a user-defined String value to an enum constant- By overriding toString() method
- By Using an instance variable
In our example, nosotros accept an enumeration type called Color, it has 3 enum constants RED, GREEN, and BLUE. By default when y'all travel past times whatsoever of these enumeration constants to System.out.println() method, it volition telephone phone toString() method of java.lang.Object. Since every enum past times default extends java.lang.Enum, which overrides toString() method.
As shown inwards next code snippet, this overridden version returns the ame, which is the literal value of enumeration constant.
/** * Returns the get upward of this enum constant, equally contained inwards the * declaration. This method may endure overridden, though it typically * isn't necessary or desirable. An enum type should override this * method when a to a greater extent than "programmer-friendly" string cast exists. * * @return the get upward of this enum constant */ public String toString() { return name; }
/** * Couple of ways to render custom String values to whatsoever enum instance inwards Java. * * @author Javin Paual */ public class EnumWithCustomString{ public static void main(String args[]) { System.out.println("Default String value of Java Enum Color.RED is " + Color.RED); System.out.println("Custom String value of Java Enum ColorWithToString.RED is " + ColorWithToString.RED); System.out.println("Custom String value of Enum inwards Java, ColorWithSpecificString.RED is " + ColorWithSpecificString.RED.getCustomString()); } /* * If y'all don't implement toString() method inwards Enum * constants, toString() volition render their get upward e.g. * RED.toString() volition render RED. This is also * same equally calling RED.name() method. */ private enum Color { RED, GREEN, BLUE; } /* * You tin implement toString() method within each enum * constant to render a custom String value. For example * inwards below instance RED.toString() volition render "Red" */ private enum ColorWithToString { RED { @Override public String toString() { return "Red"; } }, GREEN { @Override public String toString() { return "Green"; } }, BLUE { @Override public String toString() { return "Blue"; } }; } /* * You tin likewise render custom String values for Java enum, by * storing them inwards divide field. In this instance toString() will * stay unchanged, but y'all tin acquire custom String value past times calling * your method e.g. RED.toString() volition render RED, but RED.getCustomString() * volition render "Red". */ private enum ColorWithSpecificString { RED("red"), BLUE("blue"), GREEN("green"); private String custom; private ColorWithSpecificString(String custom) { this.custom = custom; } public String getCustomString() { return custom; } } } Output: Default String value of Java Enum Color.RED is RED Custom String value of Java Enum ColorWithToString.RED is Red Custom String value of Enum inwards Java, ColorWithSpecificString.RED is red
That's all nigh how to impress custom string value of enum inwards Java. As suggested past times Oracle Java developer's equally well, the correct agency to render an String other than literal value is past times overriding toString() method. This method is meant to render a String value in addition to used past times many developers to convert an Enum to String equally well. Second method likewise highlight, how y'all tin purpose enum equally amount blown class, defining instance variable, creating methods in addition to writing code there. You tin purpose this technique to associate anything amongst your enum constant inwards Java.
Further Learning
Data Structures in addition to Algorithms: Deep Dive Using Java
here)
No comments:
Post a Comment