본문

160127P(수)

Java - Chapter 5 객체와 클래스


byte, short, int, long, char, float, double

디폴트 값 : 0


boolean

디폴트 값 : false


reference etc.

디폴트 값 : null


Exception의 처리

class Account {

...


int withdraw(int amount) throws Exception {

if(balance < amount) {

throw new Exception("잔액이 부족합니다.");

}

balance -= amount;


return amount;

}

}


class MethodExample5 {

public void main(String args[]) {

...


try {

int amount = obj.withdraw(1000000);

}

catch (Exception e) {

String msg = e.getMessage();

System.out.println(msg);

}

}

}


생성자도 위와 같이 처리 가능하다.


정적 초기화 블록

정적필드는 특정 객체에 속하지 않기 때문에 생성자에서 초기값 대입이 불가능

∴ 정적 초기화블록(static initialization block OR static initializer)를 사용한다.


class HundedNumbers {

static int arr[];

static {

arr = new int[100];


for(int cnt = 0; cnt < 100; cnt++) {

arr[cnt] = cnt;

}

}

}


JVM은 이 클래스가 사용되기 전에 정적 블록안의 명령문을 실행


여러개의 정적 초기화 블록이 사용 가능하다.






Java - Chapter 7 레퍼런스 타입



'Programming > Java' 카테고리의 다른 글

170924(일) - Thread  (0) 2017.09.24
160117P(일)  (0) 2016.01.17
160109P(토)  (0) 2016.01.10
160108P(금)  (0) 2016.01.09
160107P(목)  (0) 2016.01.07

공유

댓글