๐Ÿ“ฆ 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?

  1. final static์œผ๋กœ ์ƒ์ˆ˜๋ฅผ ์„ ์–ธํ•˜๋Š” ๊ฒฝ์šฐ ์ƒ์ˆ˜ ์ด๋ฆ„๊ณผ ์ƒ์ˆ˜ ๊ฐ’ ์ž์ฒด๋Š” ๊ด€๋ จ์ด ์—†๊ฒŒ๋œ๋‹ค.
  2. ์ด๋ฆ„์˜ ์ถฉ๋Œ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‹ค์Œ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ๋ฐฐ๊ฒฝ์„ ๋” ์ •ํ™•ํžˆ ์ดํ•ดํ•ด๋ณด์ž!

 

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

  1. ์ฝ”๋“œ๊ฐ€ ๋‹จ์ˆœํ•ด์ง€๊ณ , ๊ฐ€๋…์„ฑ์ด ์ข‹์•„์ง„๋‹ค.
  2. ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ๊ณผ ์ƒ์†์„ ๋ฐฉ์ง€ํ•œ๋‹ค.
  3. 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 ์„ ์‚ฌ์šฉํ•œ ์ƒ์ˆ˜ ์„ ์–ธ์ด ๊ฐ€๋…์„ฑ ๋ฉด์—์„œ ๋” ์ข‹๋‹ค.

 

 

 

 

 

 

Enum Types (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. See Java Language Changes for a summary of updated

docs.oracle.com

 

 

11์ฃผ์ฐจ ๊ณผ์ œ: Enum

11์ฃผ์ฐจ ๊ณผ์ œ: Enum · Issue #11 · whiteship/live-study ๋ชฉํ‘œ ์ž๋ฐ”์˜ ์—ด๊ฑฐํ˜•์— ๋Œ€ํ•ด ํ•™์Šตํ•˜์„ธ์š”. ํ•™์Šตํ•  ๊ฒƒ (ํ•„์ˆ˜) enum ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ• enum์ด ์ œ๊ณตํ•˜๋Š” ๋ฉ”์†Œ๋“œ (values()์™€ valueOf()) java.lang.Enum EnumSet ๋งˆ๊ฐ์ผ์‹œ 2021

hyeonic.tistory.com

 

 

[Java] ์ž๋ฐ”์˜ ์ƒ์ˆ˜(Constant), final ๋ณ€์ˆ˜ ์ •๋ฆฌ

์ƒ์ˆ˜(Constant) ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ ์ƒ์ˆ˜๋Š” ๋ณ€ํ•˜์ง€ ๋ง์•„์•ผ ํ•  ๋ฐ์ดํ„ฐ๋ฅผ ์ž„์‹œ์ ์œผ๋กœ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ์ˆ˜๋‹จ์œผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค. ์ฆ‰, ์ดˆ๊ธฐํ™” ์ดํ›„ ์žฌํ• ๋‹น์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๋œป์ด๋‹ค. final ์ž๋ฐ”์—์„œ๋Š” ์ƒ์ˆ˜๋ฅผ ๊ตฌ

ittrue.tistory.com

 

 

Java: enum์˜ ๋ฟŒ๋ฆฌ๋ฅผ ์ฐพ์•„์„œ...

์ด๋ฒˆ ๊ธ€์—์„œ๋Š” ์ž๋ฐ” 1.5๋ฒ„์ „๋ถ€ํ„ฐ ์ƒˆ๋กญ๊ฒŒ ์ถ”๊ฐ€๋œ ์—ด๊ฑฐํ˜• ์ƒ์ˆ˜์ธ enum(enumeration)์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์—ด๊ฑฐํ˜•์€ ์„œ๋กœ ์—ฐ๊ด€๋œ ์ƒ์ˆ˜๋“ค์˜ ์ง‘ํ•ฉ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ ๊ธ€์€ enum ์ •์˜์™€ enum ์‚ฌ์šฉ๋ฐฉ๋ฒ•, ๊ทธ๋ฆฌ๊ณ  enum

www.nextree.co.kr

 

 

Java Enum ํ™œ์šฉ๊ธฐ | ์šฐ์•„ํ•œํ˜•์ œ๋“ค ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ

{{item.name}} ์•ˆ๋…•ํ•˜์„ธ์š”? ์šฐ์•„ํ•œ ํ˜•์ œ๋“ค์—์„œ ๊ฒฐ์ œ/์ •์‚ฐ ์‹œ์Šคํ…œ์„ ๊ฐœ๋ฐœํ•˜๊ณ  ์žˆ๋Š” ์ด๋™์šฑ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ ์‚ฌ๋‚ด ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŒ… ์ฃผ์ œ๋กœ ์ €๋Š” Java Enum ํ™œ์šฉ ๊ฒฝํ—˜์„ ์„ ํƒํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด์ „์— ๊ฐœ์ธ ๋ธ”๋กœ๊ทธ์— E

techblog.woowahan.com

 

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

+ Recent posts