CEN206 Object-Oriented Programming
Java enum Strings
In Java, we can get the string representation of enum constants using the toString() method or the name() method. For example,
enum Size {
SMALL, MEDIUM, LARGE, EXTRALARGE
}
class Main {
public static void main(String[] args) {
System.out.println("string value of SMALL is " + Size.SMALL.toString());
System.out.println("string value of MEDIUM is " + Size.MEDIUM.name());
}
}
- we have seen the default string representation of an enum constant is the name of the same constant.