강의

멘토링

로드맵

Inflearn brand logo image

인프런 커뮤니티 질문&답변

티티티님의 프로필 이미지
티티티

작성한 질문수

김영한의 실전 자바 - 중급 1편

예외 처리가 필요한 이유3 - 반환 값으로 예외 처리

static 사용

해결된 질문

작성

·

94

0

package exception.ex;

public class NetworkService {
    public void sendMessage(String data) {
        String url = "http://example.com";
        NetworkClient networkClient = new NetworkClient(url);

        networkClient.initError(data);

        String connectResult = networkClient.connect();
        if (isError(connectResult)) {
            System.out.println("[log] 오류 코드: " + connectResult);
        } else {
            String sendResult = networkClient.send(data);
            if (isError(sendResult)) {
                System.out.println("[log] 오류 코드: " + sendResult);
            }
        }

        networkClient.disconnect();
    }

    private static boolean isError(String resultCode) {
        return !resultCode.equals("success");
    }
}

오늘 강의 중 작성해주신 코드입니다.

isError()를 static method로 잡으신 이유가 궁금합니다.

instance method로도 가능할텐데 어떤 의도로 static 여부를 결정해야하는지 팁이 있을까요?

답변 1

0

안녕하세요. 티티티님, 공식 서포터즈 David입니다.

인스턴스 변수에 의존하지 않는다면 static 메서드를 고려해 볼 수 있습니다.

감사합니다.

티티티님의 프로필 이미지
티티티

작성한 질문수

질문하기