Keyword this and its usage

We saw several examples of its usage in a constructor similar to the following:

public SimpleMath(int i) {  this.i = i;}

It allows us to clearly distinguish between the object property and local variable, especially when they have the same name. 

Another use of the keyword this can be demonstrated in the implementation of the method equals() in the following Person class:

public class Person {  private String firstName;  private String lastName;  private LocalDate dob;  public Person(String firstName, String lastName, LocalDate dob) {    this.firstName = firstName;    this.lastName = lastName;    this.dob = dob;  }  public String getFirstName() { return firstName; }  public String getLastName() { return lastName; }  public Local ...

Get Introduction to Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.