JAVA Tutorial



THIS KEYWORD IN JAVA


The this Keyword in Java

The this keyword in Java is a reference variable that refers to the current instance of the class. It can be used to refer to the instance variables, methods, and constructors of the current object. Let's explore its use and functionality!

What is this in Java?

this refers to the current object, allowing you to access instance variables and methods of the class from within the class itself. It's commonly used when there is a need to distinguish between instance variables and method parameters that have the same name.

Basic Usage of this Keyword

The most common use of this is to differentiate between instance variables and parameters when they have the same name.

Example: Using this to Access Instance Variables

class Person {
    String name;
    int age;

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

    // Method to display the details
    public void displayDetails() {
        System.out.println("Name: " + this.name);
        System.out.println("Age: " + this.age);
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating an object of Person class
        Person person1 = new Person("Alice", 30);
        person1.displayDetails();
    }
}
  

In this example, we use this to differentiate between the instance variables name and age and the constructor parameters that have the same names.

When is this Keyword Useful?

this is especially useful in the following situations:

  • To refer to instance variables: When the instance variable name is the same as the method or constructor parameter.
  • In method chaining: To return the current object from a method to perform multiple actions on the same object.
  • In constructors: To call another constructor in the same class (constructor chaining).

Method Chaining with this

Method chaining is a technique where multiple methods are called on the same object in a single statement. We can achieve this by returning this from the methods.

Example: Method Chaining

class Car {
    String model;
    int year;

    // Setter method for model
    public Car setModel(String model) {
        this.model = model;
        return this;  // Returning the current object
    }

    // Setter method for year
    public Car setYear(int year) {
        this.year = year;
        return this;  // Returning the current object
    }

    // Method to display car details
    public void displayDetails() {
        System.out.println("Model: " + this.model);
        System.out.println("Year: " + this.year);
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating a Car object and chaining method calls
        Car myCar = new Car();
        myCar.setModel("Tesla").setYear(2023).displayDetails();
    }
}
  

Here, this is used to return the current object from both the setModel and setYear methods, allowing us to chain method calls in a single line.

Constructor Chaining with this

this can also be used to call one constructor from another within the same class. This is known as constructor chaining.

Example: Constructor Chaining

class Person {
    String name;
    int age;

    // Constructor with two parameters
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Constructor with one parameter
    public Person(String name) {
        this(name, 25);  // Calling another constructor in the same class
    }

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

public class Main {
    public static void main(String[] args) {
        // Creating an object using the constructor with one parameter
        Person person1 = new Person("John");
        person1.displayDetails();
    }
}
  

In this example, the constructor with one parameter calls the constructor with two parameters using this. This allows for constructor chaining and helps reduce redundancy.

Key Points to Remember

  • Refers to Current Object: this refers to the current instance of the class.
  • Instance Variables: this is used to refer to instance variables when they are shadowed by method parameters.
  • Method Chaining: this can be used to return the current object from methods to allow method chaining.
  • Constructor Chaining: this is used to call one constructor from another in the same class.

Quick Tip:

Use this to resolve ambiguity in constructors or methods and when chaining methods or constructors together for more efficient code.


🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review