Each object has its own copy of every instance field. A static field has exactly one copy, owned by the class, and exists even when no object does. Inside an instance method, this is the reference to the object the method was called on; inside a constructor, it is the object being built. That is how deposit knows whose balance to change.
THE MEANING OF THIS
a.deposit(x) ⇒ run deposit with this=ref(o)
Holds for every call of an instance method, where a holds ref(o). A bare field name such as balance inside deposit means this.balance, unless a parameter or local variable has the same name; then you write this.balance, as in the constructor above. A static method belongs to the class and has no this, even when called through an object.
Add a counter of accounts built. Put this field, constructor and method in the class, replacing the old constructor:
private static int count = 0;
public BankAccount(String owner, double opening) {
this.owner = owner;
this.balance = opening;
count++;
}
public static int getCount() { return count; }
Then, in a main method that builds no other account:
new BankAccount("A", 100);
new BankAccount("B", 200);
System.out.println(BankAccount.getCount()); // prints 2
The count is asked of the class, so no account variable is needed.