this Keyword
Students will understand what the Java this keyword means,
why it is useful, and how it is commonly used in classes, constructors,
and methods.
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
this?The this keyword is often used when:
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
thisLook 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.
thisMany 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.
this MattersWithout 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;
}
}
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.
this in MethodsThe 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.
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.
thisThe 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.
this Helps Avoid Confusionpublic 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.
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.
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.
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.
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.
public class Book
{
private String title;
public Book(String title)
{
title = title;
}
}
Problem: The instance variable does not get assigned.
public class Book
{
private String title;
public Book(String title)
{
this.title = title;
}
}
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.
public class Car
{
private String model;
public Car(String model)
{
this.model = model;
}
}
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
A variable declared inside a class but outside of methods.
private String name;
A variable listed inside a method or constructor heading.
public Student(String name)
The object currently using the method or constructor.
thisA Java keyword that refers to the current object.
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
What does this refer to in Java?
Answer: The current object.
Why do we use this.name = name;?
Answer: To assign the parameter name to the
instance variable name.
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.
What is the correct version?
public Student(String name)
{
this.name = name;
}
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.