← Back to Questions
Java

What is the use of this keyword in Java?

Learn What is the use of this keyword in Java? with simple explanations, real-time examples, interview tips and practical use cases.

The this keyword in Java is a reference variable that refers to the current object of the class.

In simple words:

this keyword is used to access the current class object, variables, methods, and constructors.


Why this Keyword is Important?

The this keyword helps:

  • Differentiate instance variables from local variables
  • Call current class methods
  • Invoke current class constructors
  • Pass current object as parameter
  • Return current object from methods

Main Uses of this Keyword

  • Refer current class instance variable
  • Call current class method
  • Call current class constructor
  • Pass current object as argument
  • Return current object

this Keyword Flow Diagram


Current Object

     |
     +-------> Access Variables

     |
     +-------> Call Methods

     |
     +-------> Call Constructors

     |
     +-------> Pass Object Reference


1. Refer Current Class Instance Variable

When local variable and instance variable have same name, this keyword differentiates them.


Example

class Employee {

    int id;

    Employee(int id) {

        this.id = id;

    }

}

Explanation

  • this.id → Instance variable
  • id → Local variable

Variable Assignment Flow


Constructor Parameter

      |
      v

this.id = id

      |
      +-------> Left Side → Instance Variable

      |
      +-------> Right Side → Local Variable


Without this Keyword

class Employee {

    int id;

    Employee(int id) {

        id = id;

    }

}

Problem

Local variable shadows instance variable. Instance variable remains uninitialized.


2. Call Current Class Method Using this

this keyword can invoke current class methods.


Example

class Test {

    void show() {

        System.out.println("Show Method");

    }

    void display() {

        this.show();

    }

}

Output


Show Method


Method Call Flow


Current Object

      |
      v

this.show()

      |
      v

Current Class Method Executes


3. Call Current Class Constructor Using this()

this() is used for constructor chaining within same class.


Example

class Student {

    Student() {

        this(101);

        System.out.println("Default Constructor");

    }

    Student(int id) {

        System.out.println(id);

    }

}

Output


101

Default Constructor


Constructor Chaining Flow


Student()

      |
      v

this(101)

      |
      v

Student(int id)


Important Rule of this()

this() must always be:

  • First statement inside constructor

Invalid Example

Student() {

    System.out.println("Hello");

    this(101);

}

Why Invalid?

Because this() is not first statement.


4. Pass Current Object as Argument

this keyword can pass current object to methods or constructors.


Example

class Employee {

    void display(Employee e) {

        System.out.println("Method Called");

    }

    void show() {

        display(this);

    }

}

Object Passing Flow


Current Object

      |
      v

this

      |
      v

Passed to Method


Why Useful?

  • Event handling
  • Framework callbacks
  • Dependency injection
  • Builder patterns

5. Return Current Object

this keyword can return current object reference.


Example

class Employee {

    Employee getObject() {

        return this;

    }

}

Why Useful?

Supports:

  • Method chaining
  • Fluent APIs
  • Builder design pattern

Method Chaining Example

class User {

    User setName() {

        return this;

    }

    User setEmail() {

        return this;

    }

}

Method Chaining Flow


user.setName()

      |
      v

Returns this

      |
      v

setEmail()


this vs super Keyword

Feature this super
Represents Current Object Parent Object
Used For Current Class Access Parent Class Access
Constructor Call this() super()
Variable Access Current Variables Parent Variables

this Keyword in Banking Systems

Banking applications use this keyword for:

  • Account initialization
  • Transaction setup
  • Customer profile management
  • Builder pattern implementations

Example

this.accountNumber = accountNumber;

this.balance = balance;

this Keyword in E-Commerce Systems

E-commerce platforms use this keyword for:

  • Product initialization
  • Order configuration
  • Cart management
  • Builder APIs

this Keyword in Spring Boot

Spring Boot applications frequently use this keyword for:

  • Dependency initialization
  • Bean configuration
  • Service injection
  • Constructor injection

Spring Boot Example

@Service
class UserService {

    private final UserRepository repo;

    UserService(UserRepository repo) {

        this.repo = repo;

    }

}

this Keyword Internal Working


Object Created

      |
      v

Memory Allocated

      |
      v

this Points to Current Object

      |
      v

Access Current Members


Advantages of this Keyword

  • Improves readability
  • Avoids variable ambiguity
  • Supports constructor chaining
  • Enables method chaining
  • Supports clean object-oriented design

Disadvantages

  • Excessive usage may reduce readability
  • Complex chaining can confuse beginners

Common Interview Mistake

Many developers think this refers to class name.

Actually:

  • this refers to current object instance.

Another Common Mistake

Many developers think this() and super() can be used together.

Actually:

  • Only one constructor call is allowed.
  • It must be first statement.

Invalid Example

Employee() {

    this();

    super();

}

Best Practices

  • Use this to avoid variable shadowing
  • Use constructor chaining carefully
  • Use method chaining for fluent APIs
  • Keep object initialization clean

this Keyword in Microservices

Microservices architectures use this keyword for:

  • Dependency injection
  • Configuration setup
  • Builder APIs
  • Service initialization

Realtime Enterprise Example

Builder Pattern

User.builder()
    .setName("Naresh")
    .setEmail("test@gmail.com")

Internally methods return:

this

to support chaining.


Related Learning Topics


Professional Interview Answer

The this keyword in Java is a reference variable that refers to the current object instance. It is mainly used to access current class variables, invoke current class methods, call constructors within the same class using this(), pass the current object as a method argument, and return the current object reference. The this keyword helps avoid variable ambiguity, supports constructor chaining, and enables method chaining patterns commonly used in enterprise applications, Spring Boot frameworks, builder APIs, banking systems, and microservices architectures.


Frequently Asked Questions

What does this keyword represent in Java?

this represents the current object instance.

Why is this keyword used?

To access current class variables, methods, and constructors.

What is this() in Java?

this() is used to call another constructor within the same class.

Can this keyword be returned?

Yes, this can be returned to support method chaining.

What is the difference between this and super?

this refers to current object, while super refers to parent object.

Why this Java question is important?

This interview question helps candidates understand real-time backend development concepts, practical problem solving, coding fundamentals, system design basics and production-ready application behavior.

Practice this question carefully for Java backend roles, Spring Boot developer interviews, microservices interviews, company interviews and full-stack developer preparation.

About the Author

Naresh Kumar is a Senior Java Backend Engineer with experience building enterprise applications using Java, Spring Boot, Microservices, Docker, Kubernetes and Cloud technologies.