/* Author: James Tam Version: Feb. 4, 2021 Learning objective: 1) The application of the 'this' reference. It's an implicit paramter because it's automatically accessible to all non-static methods. a) With the two mutator methods: this.attribute = value will differentiate the attribute from the parameter e.g. this.age = age; b) With the method when another Person object is passed as a parameter: displayFriend(). The this reference makes it possible to know which object's method has been called. 2) Calling other versions of a constructor via this(). */ public class Person { public static final String DEFAULT_NAME = "No name"; public static final int DEFAULT_AGE = -1; private int age; private String name; public Person() { //Calls Person(String,int) this(DEFAULT_NAME,DEFAULT_AGE); //Don't do this in this particular example. Method calls itself //endlessly. Fortunately the Java compiler catches it for you. //this(); } public Person(String newName, int newAge) { setName(newName); setAge(newAge); } public void displayFriend(Person aPerson) { System.out.print("Whose display friend method: "); System.out.println(this.name + ", " + age); System.out.println(name + "'s friend is " +aPerson.name); } public int getAge() { return(age); } public String getName() { return(name); } //Logic error, this not used. public void notSetAge(int age) { age = age; } public void setAge(int age) { this.age = age; } public void setName(String name) { this.name = name; } }