Here we will add an attribute to next example by overriding the toString() method.
Source Code
1.enum)
package com.beginner.examples;
public enum Languages {
ENGLISH("This is English"), JAPANESE("This is Japanese"), CHINESE("This is Chinese");
//
private String introduc;
private Languages(String introduc) {
this.introduc = introduc;
}
@Override
public String toString() {
return this.introduc;
}
}
2)
package com.beginner.examples;
public class OverrideToStringWithEnum {
public static void main(String[] args) {
Languages language = Languages.ENGLISH;
Languages language2 = Languages.JAPANESE;
Languages languages3 = Languages.CHINESE;
//Output these enumerated classes
System.out.println(language);
System.out.println(language2);
System.out.println(languages3);
}
}
Output:
This is English
This is Japanese
This is Chinese