더북(TheBook)

다음 절을 보기 전까지는 이 내부 클래스를 static으로 선언한 이유가 불분명해 보일 것이다. 일단은 그대로 받아들이자.

접근 제어를 제외하면 Item 클래스에는 특별한 것이 없다. 이 클래스는 Invoice 안에 비공개로 선언해서 오직 Invoice의 메서드에서만 접근할 수 있다. 그래서 여기서는 이 내부 클래스의 인스턴스 변수를 굳이 비공개로 만들지 않았다.

다음은 내부 클래스의 객체를 생성하는 메서드다.

public class Invoice {

    ...

    public void addItem(String description, int quantity, double unitPrice) {

        Item newItem = new Item();

        newItem.description = description;

        newItem.quantity = quantity;

        newItem.unitPrice = unitPrice;

        items.add(newItem);

    }

}

신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.