The this keyword serves several purposes in Java:

  1. Referring to Instance Variables: It differentiates instance variables from parameters with the same name.
  2. Invoking Other Constructors: It allows one constructor to call another constructor in the same class.
  3. Passing the Current Object: It can be used to pass the current object as a parameter to other methods or constructors.

Referring to Instance Variables

When the parameter names are the same as the instance variables, the this keyword helps to distinguish between them.

Example

  public class Person {
    String name;
    int age;

    // Constructor with parameters
    public Person(String name, int age) {
        this.name = name; // Refers to the instance variable
        this.age = age;   // Refers to the instance variable
    }

    public void display() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
    }

    public static void main(String[] args) {
        Person person = new Person("Alice", 30);
        person.display();
    }
}
  

Explanation

  • this.name refers to the instance variable name.
  • name (without this) refers to the constructor parameter.
  • Similarly, this.age refers to the instance variable age.

Invoking Other Constructors

You can use the this keyword to call another constructor in the same class, also known as constructor chaining.

Example

  public class Rectangle {
    int length;
    int width;

    // Default constructor
    public Rectangle() {
        this(1, 1); // Calls the parameterized constructor
    }

    // Parameterized constructor
    public Rectangle(int length, int width) {
        this.length = length;
        this.width = width;
    }

    public void display() {
        System.out.println("Length: " + length);
        System.out.println("Width: " + width);
    }

    public static void main(String[] args) {
        Rectangle rect1 = new Rectangle(); // Calls default constructor
        Rectangle rect2 = new Rectangle(10, 5); // Calls parameterized constructor

        rect1.display();
        rect2.display();
    }
}
  

Explanation

  • this(1, 1) in the default constructor calls the parameterized constructor with default values.
  • The parameterized constructor initializes the length and widthvariables.

Passing the Current Object

You can use this to pass the current object to another method or constructor.

Example java Copy code public class Printer { public void printPerson(Person person) { System.out.println(“Person Details:”); person.display(); } }

public class Person { String name; int age;

public Person(String name, int age) {
    this.name = name;
    this.age = age;
}

public void display() {
    System.out.println("Name: " + name);
    System.out.println("Age: " + age);
}

public static void main(String[] args) {
    Person person = new Person("Bob", 25);
    Printer printer = new Printer();
    printer.printPerson(person); // Passes the current object
}

} Explanation printer.printPerson(person); passes the current Person object to the printPerson method of the Printer class. person is an instance of Person and is passed as an argument. Example Program Here is a complete example program demonstrating the use of the this keyword in various scenarios:

java Copy code public class Car { String model; int year;

// Constructor with parameters
public Car(String model, int year) {
    this.model = model;
    this.year = year;
}

// Default constructor
public Car() {
    this("Unknown", 0); // Calls the parameterized constructor
}

public void printDetails() {
    System.out.println("Model: " + this.model);
    System.out.println("Year: " + this.year);
}

public static void main(String[] args) {
    Car car1 = new Car("Toyota", 2022);
    Car car2 = new Car(); // Uses default constructor

    car1.printDetails();
    car2.printDetails();
}

} Explanation The Car class demonstrates both referring to instance variables and invoking other constructors. The default constructor calls the parameterized constructor to initialize default values. The printDetails method uses this to refer to instance variables when printing.