Почему в нижеприведенном коде
Id tom = 1??
В конструкторе объекта написано, что id = counter++
counter =1. Сл-но id = 1+1 = 2.
Почему в этой строке :
System.out.println(Person.counter);
выводится значение 3 ??? Здесь же нет нового объекта, в котором конструктор прибавляет единицу.
public static void main(String[] args) {
Person tom = new Person();
Person bob = new Person();
tom.displayId(); // Id = 1
bob.displayId(); // Id = 2
System.out.println(Person.counter); // 3
// изменяем Person.counter
Person.counter = 8;
Person sam = new Person();
sam.displayId(); // Id = 8
}
public class Person {
private int id;
static int counter=1;
Person(){
id = counter++;
}
public void displayId(){
System.out.printf("Id: %d \n", id);
}