Skip to content

[아이템 22] 140p : 상수 인터페이스의 문제점 #32

Answered by iamsojung
jun108059 asked this question in Q&A
Discussion options

You must be logged in to vote

질문해주신 내용에 대해 생각해봤는데,, 영준님 해석이 맞는 것 같고 책에서 보면 아래와 같은 문장이 있는데요
final이 아니면 재할당해서 사용할 수 있어서 오염되는 것 같아요 .

그리고 '상수를 공개할 목적이라면 더 합당한 선택지가 몇 가지 있다. 특정 클래스나 인터페이스와 강하게 연관된 상수라면 그 클래스나 인터페이스 자체 에 추가해 야 한다.' 문장처럼.. 직접 그 상수를 사용하는 쪽에 추가해야 되는 것 같아요..!

예를 들어서, 아래 코드를 보면 TestInterFace에 NAME을 추가해뒀는데, InterfaceImpl 클래스에 또 NAME을 추가해서 쓰고 있으니까,, 그게 아니라 저 NAME이 필요한 InterfaceImpl 클래스에만! 선언해서 사용하는 방법으로 쓰라는 말 같습니다..

public interface TestInterFace {
    static String NAME = "incheol";
}
public class InterfaceImpl implements TestInterFace {
    public static final String NAME = "test";

    public static void main(String[] args) {
        System.out.println(NAME);
    }
}

// result : test

그리고 마지막에 말씀하신 것 처럼 인터페이스에…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by jun108059
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants