The Account class and its boundary
The Account class declares its private double balance field and its constructor. The constructor has the exact same name as the class and no return type.
The Account class declares its private double balance field and its constructor.
The constructor has the exact same name as the class and no return type.
The Account class declares its private double balance field and its constructor. The constructor has the exact same name as the class and no return type.
The light blue particle is the execution flow. In main, it runs new Account(1000).
Inside the constructor, this.balance = balance tells the field apart from the parameter.
The object is born on the Heap with a valid initial state: balance = 1000.
The light blue particle is the execution flow. In main, it runs new Account(1000). Inside the constructor, this.balance = balance tells the field apart from the parameter. The object is born on the Heap with a valid initial state: balance = 1000.
What happens if main tries direct access with c.balance = -500?
The field is private: the compiler blocks the access before running.
What happens if main tries direct access with c.balance = -500? The field is private: the compiler blocks the access before running.
To read the value without exposing the field, the class offers getBalance().
The getter provides read-only access: it returns 1000.0 without opening write access.
To read the value without exposing the field, the class offers getBalance(). The getter provides read-only access: it returns 1000.0 without opening write access.
To change the state, main calls c.deposit(-500) with an invalid amount.
The guard if (amount <= 0) is true and the method exits early with return false.
The attempt is rejected at the door and this.balance stays intact at 1000.
To change the state, main calls c.deposit(-500) with an invalid amount. The guard if (amount <= 0) is true and the method exits early with return false. The attempt is rejected at the door and this.balance stays intact at 1000.
Now main calls c.deposit(200) with a valid positive value.
It passes the if, adds in this.balance += amount, and confirms with return true.
Now main calls c.deposit(200) with a valid positive value. It passes the if, adds in this.balance += amount, and confirms with return true.
A class can overload its constructor with different parameter lists.
The short constructor uses this(owner, 0) on its first line to delegate.
That way a single canonical constructor concentrates initialization without repeating rules.
A class can overload its constructor with different parameter lists. The short constructor uses this(owner, 0) on its first line to delegate. That way a single canonical constructor concentrates initialization without repeating rules.
The constructor guarantees that every object is born valid from its very first line.
private blocks direct access and methods validate every state change.
Encapsulation makes each object the sole owner of its own state.
The constructor guarantees that every object is born valid from its very first line. private blocks direct access and methods validate every state change. Encapsulation makes each object the sole owner of its own state.