JAVA/Java
enum의 name()과 tostring()의 차이점
JENN_tech7
2022. 1. 10. 17:57
728x90
SMALL
The main difference between name() and toString() is that name() is a final method, so it cannot be overridden. The toString() method returns the same value that name() does by default, but toString() can be overridden by subclasses of Enum.
Therefore, if you need the name of the field itself, use name(). If you need a string representation of the value of the field, use toString().
=> 쉽게 말해 tostring은 override해서 내맘대로 커스텀이 가능하다는 얘기
default 필드이름을 쓰려면 name()을 쓰세요~
public enum WeekDay {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY;
public String toString() {
return name().charAt(0) + name().substring(1).toLowerCase();
}
}
728x90
LIST