- final : 변수의 값을 변경할 수 없도록 고정
- 바뀌지 않는 값이라면 인스턴스 변수가 아니라 static을 사용해 클래스 변수로 지정하는 것이 더 좋음
public class ConstantDemo {
private final static int APPLE = 1;
private final static int PEACH = 2;
private final static int BANANA = 3;
private final static int APPLE = 4; // 에러
// final은 값을 변경할 수 없도록 하기 때문에 중복 선언을 할 수 없음
public static void main(String[] args) {
int type = 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;
}
}
}
- 인터페이스에서 정의한 변수는 final 됨을 암시함
- 인터페이스를 사용하면 이름이 중복될 확률이 낮아짐
interface FRUIT {
int APPLE=1, PEACH=2, BANANA=3;
}
interface COMPANY {
int GOOGLE=1, APPLE=2, ORACLE=3;
}
public class ConstantDemo {
public static void main(String[] args) {
int type = FRUIT.APPLE;
switch(type){
case FRUIT.APPLE:
System.out.println(57 + " kcal");
break;
case FRUIT.PEACH:
System.out.println(34 + " kcal");
break;
case FRUIT.BANANA:
System.out.println(93 + " kcal");
break;
}
}
}
- 데이터 타입을 다르게 만들어서 비교할 수 없게 함으로써, 컴파일러가 에러를 발생하게 할 수 있음
class Fruit {
public static final Fruit APPLE = new Fruit();
public static final Fruit PEACH = new Fruit();
public static final Fruit BANANA = new Fruit();
}
class Company {
public static final Company GOOGLE = new Company();
public static final Company APPLE = new Company();
public static final Company ORACLE = new COMPANY(Company);
}
public class ConstantDemo {
public static void main(String[] args) {
if(Fruit.APPLE == Company.APPLE){
System.out.println("과일 애플과 회사 애플이 같다.");
}
}
}
"enum"
: 열거형
: 서로 연관된 상수들의 집합
- 코드가 단순해짐
enum Fruit{
APPLE, PEACH, BANANA;
}
enum Company{
GOOGLE, APPLE, ORACLE;
}
public class ConstantDemo {
public static void main(String[] args) {
if(Fruit.APPLE == Company.APPLE)
System.out.println("과일 애플과 회사 애플이 같다.");
Fruit type = Fruit.APPLE;
switch(type){
// switch 문의 조건으로 사용 가능
case APPLE:
System.out.println(57 + " kcal");
break;
case PEACH:
System.out.println(34 + " kcal");
break;
case BANANA:
System.out.println(93 + " kcal");
break;
}
}
}
- enum은 클래스이기 때문에 생성자를 가질 수 있음
enum Fruit {
APPLE, PEACH, BANANA;
Fruit() {
System.out.println("Call Constructor " + this);
}
}
- enum의 생성자는 접근 제어자로 private 만을 허용해서, 값들이 변경되지 않도록 보장함
- 인스턴스의 생성과 상속을 방지
(인스턴스화 할 수 없음)
enum Fruit {
APPLE, PEACH, BANANA;
// 상수를 만들 때마다 new Fruit()가 실행됨
public Fruit() { // 에러
System.out.println("Call Constructor " + this);
// 상수가 만들어질 때마다 생성자가 실행되기 때문에, 실행하면 3번 출력됨
}
}
- enum은 상수의 기능을 넘어서, 클래스로써 멤버나 메소드 등의 정보를 가짐으로써 더 많은 작업이 가능함
- 상수를 하나씩 꺼내서 처리할 수 있음
- 멤버 전체를 열거할 수 있음
- values( ) : enum 클래스가 가지고 있는 모든 상수 값을 배열의 형태로 반환
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;
}
}
enum Company {
GOOGLE, APPLE, ORACLE;
}
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;
}
for(Fruit f : Fruit.values()) {
System.out.println(f+", "+f.getColor());
}
// 멤버 전체를 열거할 수 있음
}
}
'onYouTube > Java' 카테고리의 다른 글
제네릭 (0) | 2021.03.27 |
---|---|
참조 (0) | 2021.03.27 |
Object 클래스 (0) | 2021.03.26 |
예외 (0) | 2021.03.26 |
다형성 (Polymorphism) (0) | 2021.03.24 |