한모로그 2023. 1. 21. 11:54

상수 필드

  • 인터페이스는 public static final 특성을 갖는 불변의 상수 필드를 멤버로 가질 수 있음


    출처, 이것이 자바다


  • 인터페이스에 선언된 필드는 모두 public static final 특성을 갖기 때문에 public static final을 생략해도 자동으로 컴파일 과정에서 붙음
  • 상수명은 대문자로 작성하되, 서로 다른 단어로 구성되어 있을 경우에는 언더바( _)로 연결
package ch08.sec03;

public interface RemoteControl {

    //상수필드. 컴파일과정에서  public static final 키워드가 자동생성됨.
    int MAX_VOLUME = 10;
    int MIN_VOLUME = 0;
}

package ch08.sec03;

public class RemoteControlExample {

    public static void main(String[] args) {
        System.out.println("리모콘 최대 볼륨: " + RemoteControl.MAX_VOLUME);
        System.out.println("리모콘 최저 볼륨: " + RemoteControl.MIN_VOLUME);
    }

}