JAVA/예제

[JAVA] ch05-22. 초기화블럭 2

밍글링글링 2017. 8. 22.
728x90
public class Ex22 {
    public static void main(String[] args){
        Product p1 = new Product();
        Product p2 = new Product();
        Product p3 = new Product();
        
        System.out.printf("p1: %d\n",p1.serialNo);
        System.out.printf("p2: %d\n",p2.serialNo);
        System.out.printf("p3: %d\n",p3.serialNo);
        
        System.out.printf("Total: %d", Product.count);
    }
}    

class Product{
    static int count = 0;
    int serialNo;
    {
        serialNo = ++count; // (=) this.serialNo = ++Product.count;
    }
}
 

728x90

'JAVA > 예제' 카테고리의 다른 글

[JAVA] ch06-02. 배열 (Array) 2  (0) 2017.08.22
[JAVA] ch06-01. 배열 (Array)  (0) 2017.08.22
[JAVA] ch05-20. 초기화블럭  (0) 2017.08.22
[JAVA] ch05-19. 생성자 4  (0) 2017.08.22
[JAVA] ch05-18. 생성자 3  (0) 2017.08.22

댓글