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
'JAVA > Java' 카테고리의 다른 글
Map에 넣은 순서대로 나오게 하는 방법 (HashMap, LinkedHashMap) (0) | 2022.03.25 |
---|---|
controller에서 url로 동적 매핑하기 @PathVariable (0) | 2022.03.25 |
session time변경 (0) | 2021.12.02 |
@SuppressWarnings("rawtypes")는 뭘까 (0) | 2021.06.17 |
20201123 _스프링 톰캣 맛보기 (0) | 2020.11.23 |
댓글