Calling the object's own methods
A method can call the other methods of its own object. Inside the class, this.isFull() calls isFull on the same object the current method was called on. This bus uses that:
public class Main {
public static void main(String[] args) {
Bus bus = new Bus(2);
bus.board();
System.out.println(bus);
bus.board();
bus.board();
System.out.println(bus);
}
}public class Bus {
private int seats;
private int passengers;
public Bus(int seats) {
this.seats = seats;
this.passengers = 0;
}
public int seatsLeft() {
return this.seats - this.passengers;
}
public boolean isFull() {
return this.seatsLeft() == 0;
}
public void board() {
if (this.isFull()) {
System.out.println("Sorry, the bus is full");
} else {
this.passengers++;
}
}
public String toString() {
return this.passengers + " on board, seats left: " + this.seatsLeft();
}
}1 on board, seats left: 1
Sorry, the bus is full
2 on board, seats left: 0seatsLeft()works out the free seats.isFull()asksseatsLeft()instead of doing the sum again, andboard()asksisFull()before it lets anyone on.- Each rule is written in one place. If the bus took standing passengers one day, only
seatsLeftwould change, andisFull,boardandtoStringwould all follow. this.in front of the call can be left out: inside the class,isFull()means the same. As with instance variables, writing it shows that the method belongs to this object.
A method that only the class itself needs can be private, like the instance variables. If nothing outside the bus used seatsLeft, it could be private int seatsLeft(): isFull and toString could still call it, but main couldn't.