I am struggling to understand a specific usage of "this" keyword. What I don't understand here is why the printPerson()
method invokes the toString()
method.
public class Person {
private String name;
private int age;
public Person(String aName, int anAge) {
name = aName;
age = anAge;
}
/** @return the String form of this person */
public String toString() {
return name + " " + age;
}
public void printPerson() {
System.out.println(this);
}
}
public class Driver {
public static void main(String[] args) {
Person mete = new Person("Mete", 21);
mete.printPerson();
}
}