๐ฆ Enum
An enum type is a special data type that enables for a variable to be a set of predefined constants.
enum type์ ๋ฏธ๋ฆฌ ์ ์๋ ์์์ ์งํฉ์ด ๋ ์ ์๋๋ก ํ๋ ํน๋ณํ ๋ฐ์ดํฐ ํ์ ์ด๋ค.
The variable must be equal to one of the values that have been predefined for it.
๋ณ์๋ ๋ฏธ๋ฆฌ ์ ์๋ ๊ฐ ์ค ํ๋์ ๊ฐ์์ผ ํฉ๋๋ค.
Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.
์ผ๋ฐ์ ์ธ ์๋ก๋ ๋์นจ๋ฐ ๋ฐฉํฅ(NORTH, SOUTH, EAST ๋ฐ WEST ๊ฐ)๊ณผ ์์ผ์ด ํฌํจ๋ฉ๋๋ค.
๋ฐฉํฅ ๋์ Day๋ก ์์๋ฅผ ๋ค์ด๋ณด๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}
Day.SUNDAY // SUNDAY
enum์ ์ด๊ฑฐ์ฒด๋ฅผ ๋น๊ตํ ๋ ์ค์ ๊ฐ๋ฟ๋ง ์๋๋ผ ํ์ ๊น์ง๋ ์ฒดํฌํ๋ค.
enum์ ์์ซ๊ฐ์ด ์ฌ์ ์๋๋๋ผ๋ ๋ค์ ์ปดํ์ผํ ํ์๊ฐ ์๋ค.
enum Color {
RED(3),
YELLOW(4),
BLUE(5);
private final int value;
Color(int value) { this.value = value; }
public int getValue() { return value; }
}
System.out.println(Color.RED.name()); // RED
System.out.println(Color.RED.value()); // 3
1. ์๋ฐ์์ ์์ ์ ์ํ๊ธฐ:: static final
์์๋ฅผ ์ ์ํ๋ ๋ฐฉ๋ฒ์๋ enum์ด ๋ํ๋๊ธฐ ์ ์ static final์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ด ์์๋ค.
static final์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ ์์๋ ๋ค์๊ณผ ๊ฐ๋ค.
์์ ์ํฉ์ผ๋ก ์ฌ์ฉ์๋ 1์์ 100์ฌ์ด์ ๊ฐ๋ง ์ ๋ ฅํ ์ ์๋ ํ๋ก๊ทธ๋จ์ด ์๋ค๊ณ ํ์.
์ฌ๊ธฐ์ 1๊ณผ 100์ ์์๋ก ์ ์ธํ ๊ฒ์ด๋ค. static final์ ํ์ฉํ ์ฝ๋๋ ์๋์ ๊ฐ๋ค.
public class Application {
private static final int MIN_SIZE = 1;
private static final int MAX_SIZE = 100;
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int userInput = parseInt(in.readLine());
if(userInput >= MIN_SIZE
&& userInput <= MAX_SIZE) {
System.out.println("good");
}
}
}
์ด๋ฌํ static final ์ด ์กด์ฌํ์๋๋ฐ ์ enum ์ด๋ผ๋ ์ด๊ฑฐํ์ด ๋์์๊น?
Why created the enum?
- final static์ผ๋ก ์์๋ฅผ ์ ์ธํ๋ ๊ฒฝ์ฐ ์์ ์ด๋ฆ๊ณผ ์์ ๊ฐ ์์ฒด๋ ๊ด๋ จ์ด ์๊ฒ๋๋ค.
- ์ด๋ฆ์ ์ถฉ๋์ด ๋ฐ์ํ ์ ์๋ค.
๋ค์ ์์๋ฅผ ํตํด ๋ฐฐ๊ฒฝ์ ๋ ์ ํํ ์ดํดํด๋ณด์!
final static์ผ๋ก ์์๋ฅผ ์ ์ธํ๋ ๊ฒฝ์ฐ ์์ ์ด๋ฆ๊ณผ ์์ ๊ฐ ์์ฒด๋ ๊ด๋ จ์ด ์๊ฒ๋๋ค.
public class Application {
private static final String SPRING = "๋ด";
private static final String SUMMER = "์ฌ๋ฆ";
private static final String AUTUMN = "๊ฐ์";
private static final String WINTER = "๊ฒจ์";
public static void main(String[] args) {
System.out.println(SPRING); // ๋ด
System.out.println(SUMMER); // ์ฌ๋ฆ
System.out.println(AUTUMN); // ๊ฐ์
System.out.println(WINTER); // ๊ฒจ์ธ
String season = "๋ด";
if (season == SPRING) {
System.out.printf("season is %s", SPRING); // season is ๋ด
}
}
}
์ด๋ฆ์ ์ถฉ๋์ด ๋ฐ์ํ ์ ์๋ค.
public class Framework {
public static final int SPRING = 1;
public static final int VUE = 2;
public static final int NESTJS = 3;
}
public class Season {
public static final int SPRING = 1;
public static final int SUMMER = 2;
public static final int AUTUMN = 3;
public static final int WINTER = 4;
}
// Season.SPRING Framework.SPRING ์ถฉ๋
Advantages of using Enum instead of static final
- ์ฝ๋๊ฐ ๋จ์ํด์ง๊ณ , ๊ฐ๋ ์ฑ์ด ์ข์์ง๋ค.
- ์ธ์คํด์ค ์์ฑ๊ณผ ์์์ ๋ฐฉ์งํ๋ค.
- enum ํค์๋ ์ฌ์ฉ์ ํตํด์ ๊ตฌํ ์๋๊ฐ ์ด๊ฑฐํ์์ ๋ถ๋ช ํ๊ฒ ๋ํ๋ผ ์ ์๋ค.
static final ์ฌ์ฉ
private static final String SPRING = "๋ด";
private static final String SUMMER = "์ฌ๋ฆ";
private static final String AUTUMN = "๊ฐ์";
private static final String WINTER = "๊ฒจ์";
enum ์ฌ์ฉ
public enum Season {
SPRING("๋ด"),
SUMMER("์ฌ๋ฆ"),
AUTUMN("๊ฐ์"),
WINTER("๊ฒจ์ธ");
private final String value;
Seson(String value) { this.value = value; }
public String getValue() {
return value;
}
}
final static ์ ์ฌ์ฉํ์ฌ ์ ์ธํ ์์๋ณด๋ค enum ์ ์ฌ์ฉํ ์์ ์ ์ธ์ด ๊ฐ๋ ์ฑ ๋ฉด์์ ๋ ์ข๋ค.
728x90
'Language > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JAVA] Vector Class (1) | 2023.11.29 |
---|---|
[JAVA] Getter/Setter (0) | 2023.11.02 |
[JAVA] ์์ฑ์ (0) | 2023.10.25 |
[JAVA] ๊ฐ ๋ณต์ฌ์ ์ฃผ์ ๋ณต์ฌ (0) | 2023.10.25 |
[JAVA] Java Collections / Array / ArrayList (2) | 2023.10.25 |