[!INDEX]

  1. Definition
  2. To refer to instance variables of the current object
  3. To call one constructor from another (Constructor Chaining) + approach
  4. To pass the current object as an argument + approach
  5. To return the current object from a method + approach, setname to the employee.
  6. Why is this useful?
  7. To invoke the current class's method.+approach
  8. To access the current class's instance from an inner class
  9. Summary of this Usage:

1. Definition

[!NOTE]

The this keyword in Java is a special reference to the current instance of the class in which it is used. It allows a method or constructor to refer to the instance variables, methods, or constructors of the current object. It is commonly used for various purposes in object-oriented programming.

Here are the main uses of this keyword in Java:


2. To refer to instance variables of the current object

[!NOTE]

When a local variable (e.g., in a method or constructor) has the same name as an instance variable, the this keyword can be used to differentiate between them.

class Employee {
    private String name;
    Employee(String name) {
        this.name = name;  // 'this.name' refers to the instance variable
    }
    void display() {
        System.out.println("Employee Name: " + this.name);  // Referring to instance variable
    }
}
public class Main {
    public static void main(String[] args) {
        Employee emp = new Employee("John");
        emp.display();  // Output: Employee Name: John
    }
}

3. To call one constructor from another (Constructor Chaining) + approach

[!NOTE] In Java, this() can be used to call another constructor from the current constructor. It helps in reducing code duplication by allowing constructors to reuse other constructors.

class Employee {
    private String name;
    private int age;

    // Constructor 1
    Employee(String name) {
        this(name, 0);  // Calling constructor 2
    }

    // Constructor 2
    Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }

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

public class Main {
    public static void main(String[] args) {
        Employee emp1 = new Employee("John");
        Employee emp2 = new Employee("Jane", 30);

        emp1.display();  // Output: Employee Name: John, Age: 0
        emp2.display();  // Output: Employee Name: Jane, Age: 30
    }
}

[!approach]

  1. make employee's class object
  2. call both ct , first ct(name,0)

4. To pass the current object as an argument + approach

[!NOTE] The this keyword can be used to pass the current instance of a class to another method or constructor.

class Employee {
    String name;
    Employee(String name) {
        this.name = name;  // Setting the name for the current object i.e. emp
    }
    void display(Employee emp) { // Employee object as an argument
        System.out.println("Employee Name: " + emp.name);
    }
    // Method in the same class that passes 'this' as an argument
    void show() {
        display(this);  // Passing the current object to display method
    }
}
public class Main {
    public static void main(String[] args) {
        Employee emp = new Employee("John");
        emp.show();
    }
}

[!important] When you pass the current object as an argument using the this keyword, it means you're passing the reference of the current instance of the class to a method or constructor.

[!approach]

  1. make employee class object
  2. pass to mt display
  3. call mt show,
  4. body = display (this)

5. To return the current object from a method + approach, setname to the employee

[!NOTE] this can be used to return the current class instance from a method, commonly used in method chaining.

class Employee {
    private String name;
    Employee setName(String name) { // coz return type is Employee, instead of void
        this.name = name; // 'this' refers to the current object (which is 'emp') 
        return this; // Returning the current object (which is 'emp')
    }
    void display() {
        System.out.println("Employee Name: " + name);
    }
}

public class Main {
    public static void main(String[] args) {
        Employee emp = new Employee();
        emp.setName("John"); // sets emp.name to "John" and returns 'emp' itself (this) 
    // Calling display() on the returned object (which is still emp) 
        emp.display(); // This prints: "Employee Name: John"
    // OR CAN DO THE FOLLOWING WAY , METHOD CHAINING
        // Method chaining using 'this'
        emp.setName("John").display();  // Output: Employee Name: John
    }
}
Employee Name is John

[!approach]

  1. make employee's class object
  2. call setName(), which return current class object using this
  3. method chaining can be used
  4. normal code , only change with the return type and return.

6. Why is this useful?

[!important]

What does this do here?

  1. this refers to the current object (emp) inside the setName() method.
  2. By returning this from setName(), you’re returning the same emp object, allowing the method chain to continue with .display() on the same object. Method chaining makes code concise and readable, especially when you need to set multiple properties or call multiple methods on the same object. Instead of writing:

emp.setName("John"); emp.display();

you can write it in one line:

emp.setName("John").display();


7. To invoke the current class's method.+approach

[!NOTE] this can be used to invoke the current class's method (usually if you need to clarify that you're calling the current object's method, especially when using inner classes).

class Employee {
    void display() {
        System.out.println("Inside display method");
    }

    void show() {
        this.display();  // Calling the current class's method
    }
}

public class Main {
    public static void main(String[] args) {
        Employee emp = new Employee();
        emp.show();  // Output: Inside display method
    }
}

[!approach]

  1. make object of employee class
  2. call show()
  3. show body = this.display().

8. To access the current class's instance from an inner class

In case of inner classes, this can be used to get the reference of the outer class object.

class Outer {
    private String name = "OuterClass";

    class Inner {
        void display() {
            System.out.println("Name from outer class: " + Outer.this.name);  // Accessing outer class instance
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Outer outer = new Outer();
        Outer.Inner inner = outer.new Inner();
        inner.display();  // Output: Name from outer class: OuterClass
    }
}
Name from outer class: OuterClass

9. Summary of this Usage:

[!NOTE]

  1. Refers to the current class instance variables when there is a name conflict with local variables.
  2. Calls another constructor in the same class using this().
  3. Passes the current object as an argument to methods or constructors.
  4. Returns the current object from a method.
  5. Invokes the current class's method explicitly.
  6. Accesses the outer class instance from an inner class using OuterClass.this.