Java Class Notes: The this Keyword

Learning Goal

Students will understand what the Java this keyword means, why it is useful, and how it is commonly used in classes, constructors, and methods.

1. What Is the this Keyword?

In Java, the keyword this refers to the current object.

The current object means the object that is currently using the method or constructor.

this

means:

this object

or:

the object I am currently inside of

2. Why Do We Need this?

The this keyword is often used when:

  1. A parameter name is the same as an instance variable name.
  2. We want to clearly refer to the current object’s data.
  3. We want to call another constructor in the same class.
  4. We want to pass the current object to another method.

The most common use in high school Java is to tell the difference between an instance variable and a parameter.

private String name;

and:

String name

3. Example Without this

Look at this class:

public class Student
{
    private String name;
    private int grade;

    public Student(String studentName, int studentGrade)
    {
        name = studentName;
        grade = studentGrade;
    }
}

This works because the parameter names are different from the instance variable names.

Instance variables:

private String name;
private int grade;

Parameters:

String studentName
int studentGrade

Since the names are different, Java knows what we mean.

4. Example With this

Many programmers like to use the same names for parameters and instance variables.

public class Student
{
    private String name;
    private int grade;

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

Here is what this means:

this.name = name;

The left side, this.name, means the instance variable that belongs to the current object.

The right side, name, means the parameter passed into the constructor.

So this line means:

Set the object's name equal to the parameter name.

5. Why this Matters

Without this, this code would not work correctly:

public class Student
{
    private String name;

    public Student(String name)
    {
        name = name;
    }
}

This looks correct, but it has a problem. Both names refer to the parameter, not the instance variable.

name = name;

means:

parameter = parameter;

The instance variable does not get updated.

Correct version:

public class Student
{
    private String name;

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

6. Visual Example

public class Dog
{
    private String name;
    private int age;

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

    public void printInfo()
    {
        System.out.println(this.name + " is " + this.age + " years old.");
    }
}

If we create a Dog object:

Dog myDog = new Dog("Buddy", 5);
myDog.printInfo();

Output:

Buddy is 5 years old.

In this example, this.name refers to the name variable inside the current Dog object.

this.age refers to the age variable inside the current Dog object.

7. this in Methods

The this keyword can also be used inside methods.

public class BankAccount
{
    private double balance;

    public BankAccount(double balance)
    {
        this.balance = balance;
    }

    public void deposit(double amount)
    {
        this.balance = this.balance + amount;
    }

    public void withdraw(double amount)
    {
        this.balance = this.balance - amount;
    }

    public double getBalance()
    {
        return this.balance;
    }
}

The line:

this.balance = this.balance + amount;

means:

Take the current object's balance and add the amount to it.

8. Do You Always Need this?

No. The following code works:

public void deposit(double amount)
{
    balance = balance + amount;
}

Java understands that balance is the instance variable because there is no local variable or parameter named balance.

However, some programmers still write:

this.balance = this.balance + amount;

because it makes the code very clear.

9. Most Common Use of this

The most common use of this is in constructors.

public class Rectangle
{
    private int length;
    private int width;

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

This is very common Java style. The constructor parameters have the same names as the instance variables.

10. this Helps Avoid Confusion

Confusing Version

public class Player
{
    private String name;
    private int score;

    public Player(String n, int s)
    {
        name = n;
        score = s;
    }
}

This works, but n and s are not very descriptive.

Clear Version

public class Player
{
    private String name;
    private int score;

    public Player(String name, int score)
    {
        this.name = name;
        this.score = score;
    }
}

This version is easier to read because the parameter names clearly describe what they represent.

11. Calling Another Constructor with this

The this keyword can also call another constructor in the same class. This is called constructor chaining.

public class Student
{
    private String name;
    private int grade;

    public Student()
    {
        this("Unknown", 9);
    }

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

This constructor:

public Student()
{
    this("Unknown", 9);
}

calls this constructor:

public Student(String name, int grade)

So this code:

Student s1 = new Student();

creates a student with:

name = "Unknown"
grade = 9

Important Rule: this(...) must be the first line inside the constructor.

12. Using this to Pass the Current Object

Sometimes this can be used to pass the current object to another method.

public class Student
{
    private String name;

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

    public void printStudent()
    {
        printObject(this);
    }

    public void printObject(Student s)
    {
        System.out.println(s.name);
    }
}

In this example:

printObject(this);

means:

Pass this current Student object into the method.

This is less common for beginners, but it is still important to understand.

13. AP Computer Science A Style Example

public class Book
{
    private String title;
    private String author;
    private int pages;

    public Book(String title, String author, int pages)
    {
        this.title = title;
        this.author = author;
        this.pages = pages;
    }

    public String getTitle()
    {
        return this.title;
    }

    public String getAuthor()
    {
        return this.author;
    }

    public int getPages()
    {
        return this.pages;
    }

    public void setPages(int pages)
    {
        this.pages = pages;
    }
}

In the constructor, this.title = title; assigns the parameter title to the object’s instance variable.

In the setter method, this.pages = pages; updates the object’s pages instance variable.

14. Common Student Mistake

Mistake

public class Book
{
    private String title;

    public Book(String title)
    {
        title = title;
    }
}

Problem: The instance variable does not get assigned.

Correct Version

public class Book
{
    private String title;

    public Book(String title)
    {
        this.title = title;
    }
}

15. Another Common Mistake

Mistake

public class Car
{
    private String model;

    public Car(String model)
    {
        this.model = this.model;
    }
}

Problem: This assigns the instance variable to itself. It does not use the parameter.

Correct Version

public class Car
{
    private String model;

    public Car(String model)
    {
        this.model = model;
    }
}

16. Practice Example

What does the following code print?

public class Game
{
    private int score;

    public Game(int score)
    {
        this.score = score;
    }

    public void addPoints(int score)
    {
        this.score = this.score + score;
    }

    public void printScore()
    {
        System.out.println(this.score);
    }
}
Game g = new Game(10);
g.addPoints(5);
g.printScore();

Output:

15

Explanation:

The object starts with:

score = 10

Then this method runs:

this.score = this.score + score;

The instance variable becomes:

10 + 5 = 15

17. Key Vocabulary

Instance Variable

A variable declared inside a class but outside of methods.

private String name;

Parameter

A variable listed inside a method or constructor heading.

public Student(String name)

Current Object

The object currently using the method or constructor.

this

A Java keyword that refers to the current object.

18. Simple Rule to Remember

Use this when an instance variable and a parameter have the same name.

Example:

this.name = name;

Think of it as:

this object's name = the parameter name

19. Quick Check Questions

Question 1

What does this refer to in Java?

Answer: The current object.

Question 2

Why do we use this.name = name;?

Answer: To assign the parameter name to the instance variable name.

Question 3

What is wrong with this code?

public Student(String name)
{
    name = name;
}

Answer: Both names refer to the parameter. The instance variable is not assigned.

Question 4

What is the correct version?

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

20. Summary

The Java this keyword refers to the current object.

It is most commonly used to distinguish between instance variables and parameters that have the same name.

Example:

this.name = name;

The left side is the object’s instance variable. The right side is the constructor or method parameter.

The this keyword makes Java code clearer, especially when writing constructors and setter methods.