Chapter 30 of 57

this Keyword in Java

When working with classes and objects, you will often see the this keyword. At first, it can look a little confusing, but the basic idea is actually quite simple.

In Java, this refers to the current object.

In other words, when an object is using a method or constructor, this refers to that particular object.

Why Do We Need this?

Let's look at a common situation.

Suppose we have a Student class:

class Student {
    String name;
    int age;

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

Here we have two name variables:

String name;

and:

Student(String name, int age)

The first name is the instance variable belonging to the object, while the second name is the constructor parameter.

So when we write:

this.name = name;

this.name means:

The name variable belonging to the current object.

And the name on the right side means:

The name parameter received by the constructor.

So this:

this.name = name;

means "store the parameter value inside the current object's name variable."

A Simple Example

Let's create a Student object:

class Student {
    String name;
    int age;

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

Now:

Student student = new Student("John", 20);

The constructor receives:

name → "John"
age  → 20

Then:

this.name = name;
this.age = age;

stores those values inside the newly created object.

So the object ends up with:

name → John
age  → 20

this Refers to the Current Object

The easiest way to understand this is to think of it as "this particular object."

For example:

class Student {
    String name;

    void showName() {
        System.out.println(this.name);
    }
}

If we create:

Student student = new Student();
student.name = "John";

student.showName();

then this.name refers to the name belonging to the student object.

Output:

John

If another object calls the same method, this refers to that object instead.

Student student1 = new Student();
student1.name = "John";

Student student2 = new Student();
student2.name = "Jason";

student1.showName();
student2.showName();

Output:

John
Jason

So this changes depending on which object is currently calling the method.

Using this in Methods

We can also use this inside normal methods.

class Student {
    String name;

    void setName(String name) {
        this.name = name;
    }

    void showName() {
        System.out.println(this.name);
    }
}

Now:

Student student = new Student();

student.setName("John");
student.showName();

Output:

John

Again, this.name refers to the object's instance variable, while name refers to the method parameter.

Calling Another Method Using this

We can also use this to call another method of the same object.

For example:

class Student {

    void greet() {
        System.out.println("Hello!");
    }

    void introduce() {
        this.greet();
        System.out.println("My name is John.");
    }
}

Now:

Student student = new Student();
student.introduce();

Output:

Hello!
My name is John.

Technically, we could simply write:

greet();

instead of:

this.greet();

Both work here. Using this simply makes it explicit that we are calling the method belonging to the current object.

Passing the Current Object

this can also be passed as an argument to another method.

For example:

class Student {

    void display(Student student) {
        System.out.println("Student object received.");
    }

    void sendObject() {
        display(this);
    }
}

Here:

display(this);

passes the current Student object to the display() method.

You don't need to use this technique often as a beginner, but it's useful to know that this represents the current object and can therefore be used wherever an object reference is expected.

this in Constructors

The most common place you'll see this is inside constructors.

For example:

class Car {
    String brand;
    int year;

    Car(String brand, int year) {
        this.brand = brand;
        this.year = year;
    }
}

When we create:

Car car = new Car("Toyota", 2025);

this refers to the newly created car object.

So:

this.brand = brand;

stores "Toyota" in the object's brand field.

The Main Idea

Don't try to memorize every use of this right away. The most important thing to remember is:

this refers to the current object.

You'll most commonly see it when a constructor or method parameter has the same name as an instance variable:

class Student {
    String name;

    Student(String name) {
        this.name = name;
    }
}

Here, this.name means the object's name, while name means the parameter.

Once you understand that distinction, the this keyword becomes much easier to understand.