enum은 열거형(enumerated type)
열거형은 서로 연관된 상수들의 집합이라고 할 수 있다.
enum Fruit{
APPLE, PEACH, BANANA;
}
enum은 class, interface와 동급의 형식을 가지는 단위다. 하지만 enum은 사실상 class이다. 편의를 위해서 enum만을 위한 문법적 형식을 가지고 있기 때문에 구분하기 위해서 enum이라는 키워드를 사용하는 것이다. 위의 코드는 아래 코드와 사실상 같다.
class Fruit{
public static final Fruit APPLE = new Fruit();
public static final Fruit PEACH = new Fruit();
public static final Fruit BANANA = new Fruit();
private Fruit(){}
}
생성자의 접근 제어자가 private이다. 그것이 클래스 Fruit를 인스턴스로 만들 수 없다는 것을 의미한다. 다른 용도로 사용하는 것을 금지하고 있는 것이다.
if(Fruit.APPLE == Company.APPLE){
System.out.println("과일 애플과 회사 애플이 같다.");
}
enum이 서로 다른 상수 그룹에 대한 비교를 컴파일 시점에서 차단할 수 있다는 것을 의미한다.
enum과 생성자
enum은 사실 클래스다.
enum Fruit{
APPLE, PEACH, BANANA;
Fruit(){
System.out.println("Call Constructor "+this);
}
}
결과는 아래와 같다.
Call Constructor APPLE
Call Constructor PEACH
Call Constructor BANANA
57 kcal
Call Constructor가 출력된 것은 생성자 Fruit가 호출되었음을 의미한다. 이것이 3번 호출되었다는 것은 필드의 숫자만큼 호출되었다는 뜻이다. 즉 enum은 생성자를 가질 수 있다.
하지만 코드를 아래와 같이 바꾸면 컴파일 에러가 발생한다.
enum Fruit{
APPLE, PEACH, BANANA;
public Fruit(){
System.out.println("Call Constructor "+this);
}
}
이것은 enum의 생성자가 접근 제어자 private만을 허용하기 때문이다. 덕분에 Fruit를 직접 생성할 수 없다. 그렇다면 이 생성자의 매개변수를 통해서 필드(APPLE..)의 인스턴스 변수 값을 부여 할 수 있다는 말일까? 있다. 그런데 방식이 좀 생경하다.
enum Fruit{
APPLE("red"), PEACH("pink"), BANANA("yellow");
public String color;
Fruit(String color){
System.out.println("Call Constructor "+this);
this.color = color;
}
}
public class ConstantDemo {
public static void main(String[] args) {
Fruit type = Fruit.APPLE;
switch(type){
case APPLE:
System.out.println(57+" kcal, "+Fruit.APPLE.color);
break;
case PEACH:
System.out.println(34+" kcal"+Fruit.PEACH.color);
break;
case BANANA:
System.out.println(93+" kcal"+Fruit.BANANA.color);
break;
}
}
}
실행결과는 아래와 같다.
Call Constructor APPLE
Call Constructor PEACH
Call Constructor BANANA
57 kcal, red
아래 코드는 Fruit의 상수를 선언하면서 동시에 생성자를 호출하고 있다.
APPLE("red"), PEACH("pink"), BANANA("yellow");
enum Fruit{
APPLE("red"), PEACH("pink"), BANANA("yellow");
private String color;
Fruit(String color){
System.out.println("Call Constructor "+this);
this.color = color;
}
String getColor(){
return this.color;
}
}
public class ConstantDemo {
public static void main(String[] args) {
Fruit type = Fruit.APPLE;
switch(type){
case APPLE:
System.out.println(57+" kcal, "+Fruit.APPLE.getColor());
break;
case PEACH:
System.out.println(34+" kcal"+Fruit.PEACH.getColor());
break;
case BANANA:
System.out.println(93+" kcal"+Fruit.BANANA.getColor());
break;
}
}
}
열거형의 특성을 정리해보자. 열거형은 연관된 값들을 저장한다. 또 그 값들이 변경되지 않도록 보장한다. 뿐만 아니라 열거형 자체가 클래스이기 때문에 열거형 내부에 생성자, 필드, 메소드를 가질 수 있어서 단순히 상수가 아니라 더 많은 역할을 할 수 있다.
'JAVA > 지식' 카테고리의 다른 글
뮤텍스(Mutex)와 세마포어(Semaphore)의 차이 (0) | 2021.02.09 |
---|---|
[AOP] AOP 정의, 용어정리, 활용방법 (0) | 2021.02.08 |
[Java] TimeUnit.Sleep 과 Thead.Sleep 차이 (0) | 2021.01.28 |
[Java 동기화] Synchronized 개념 정리#1 (0) | 2021.01.28 |
[접근 지정자] public, protected, default, private (0) | 2021.01.28 |