객체를 생성할 때마다 id 번호를 하나씩 늘려가며 생성해야하는 경우
일반적인 객체 변수로는 어렵다. static을 사용해서 객체들과 무관한 변수 만들기.
class A {
int id;
static int count = 1; // 1번부터 생성
A() {
id = count++; // id 부여 후 count 하나 증가
}
}
public class Main {
public static void main(String[] args) {
A a = new A();
System.out.println(a.id); // 1
A b = new A();
System.out.println(b.id); // 2
}
}