Skip to content
Discussion options

You must be logged in to vote

저도 공부하면서 단순히 valueOf가 from과 of의 자세한 버전이라길래 많이 헷갈렸던 것 같습니다..

그래서 조금 찾아본 결과 다음과 같이 이해하였습니다.

public enum Color {
  RED,
  BLUE;
}
...
Color redColor = Color.valueOf("RED");
Color blueColor = Color.valueOf("BLUE");

위와 같이 valueOf의 경우에는 enum의 요소를 "조회"할 때에 사용되는 메서드 입니다.

미리 생성된 객체를 "조회"하는 메서드이기 때문에 팩토리의 역할을 한다고 볼 수는 없지만,

외부에서 원하는 객체를 반환해주고 있으므로 결과적으로는 정적 팩토리 메서드라고 간주할 수 있습니다.

즉, from과 of와 달리 valueOf는 enum에서만 쓰이는 특정한 정팩메로 이해하면 좋을 것 같습니다!

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@digitpic
Comment options

digitpic Dec 26, 2024
Maintainer Author

@JHZLO
Comment options

Answer selected by digitpic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
질문
Labels
None yet
2 participants