티스토리 뷰
enum 열거형
- enum은 연관된 상수들의 집합(a set of named constants.)으로 열거 순서에 따라 index번호를 부여받는다.
- enum은 private constructor가 default이기에 public생성자의 생성이 금지된다 인스턴스를 만들 수 없다는 것은 즉, 다른 용도의 사용을 금한다는 의미이다.
Example Code
enum Fruit{
APPLE, PEACH, BANANA;
Fruit(){
System.out.println("Constructor");
}
}
public class Main {
public static void main(String[] args) {
Fruit type = Fruit.APPLE;
switch(type){
case APPLE:
System.out.println(57+" kcal");
break;
case PEACH:
System.out.println(34+" kcal");
break;
case BANANA:
System.out.println(93+" kcal");
break;
}
}
}
//output
//Constructor
//Constructor
//Constructor
//57 kcal
위의 enum 클래스는 아래와 같은 의미이다. 결국 enum이나 interface나 인스턴스 생성은 불가능하며 public static final 로 다른 곳에서 변수를 자유롭게 사용한다 단, 수정은 불가
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(){}
}
Reference
https://opentutorials.org/course/1223/6091
https://www.youtube.com/watch?v=sI4utYmh7O4
'JAVA' 카테고리의 다른 글
File I/O - loading data and creating txt file from CSV file. (0) | 2019.07.05 |
---|---|
Convert Array to ArrayList, ArrayList to Set (0) | 2019.07.01 |
Exception handling in Java (0) | 2019.06.24 |
Object type casting(upcasting,downcasting) example in Java (0) | 2019.06.17 |
StringBuffer in Java (0) | 2019.03.05 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 알고리즘
- spring boot application
- Collection Framework
- Object type casting
- 프로그래머스 알고리즘
- compareTo()
- rest parameter
- easy javascript algorithm
- string class in java
- hackerrank solution
- HashMap
- hackerrank
- Javascript Algorithm
- hackerrank javascript solution
- C++
- java
- code refactoring
- equals()
- math.abs
- ... in Javascript
- HackerRank Algorithm
- javascript
- repeat()
- algorithm
- hackerrank javascript
- substring()
- 프로그래머스
- easy algorithm
- 프로그래머스 알고리즘문제
- math.max
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함