In Java, the this keyword refers to the current
object. It is commonly used to access instance variables,
call methods, and invoke another constructor in the same class.
The this keyword represents the current
object whose instance method or constructor is executing.
class Student {
String name = "Rahul";
void display() {
System.out.println(this.name);
}
public static void main(String[] args) {
Student s1 = new Student();
s1.display();
}
}
Rahul
Here, this.name accesses the
name field of the current object.
We can use this to access instance
variables explicitly.
class Employee {
String name;
int salary;
Employee(String employeeName, int employeeSalary) {
this.name = employeeName;
this.salary = employeeSalary;
}
void display() {
System.out.println(name);
System.out.println(salary);
}
public static void main(String[] args) {
Employee e1 = new Employee("Amit", 30000);
e1.display();
}
}
Amit
30000
The expression this.name refers to the
object's field, while employeeName is
the constructor parameter.
A common use of this is to distinguish
an instance variable from a parameter with the same name.
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
void display() {
System.out.println(name + " " + age);
}
public static void main(String[] args) {
Student s1 = new Student("Priya", 21);
s1.display();
}
}
Priya 21
this, a parameter with the same
name can hide the instance field inside the constructor.
You can use this to call another
instance method of the same object.
class Demo {
void first() {
System.out.println("First method");
}
void second() {
this.first();
System.out.println("Second method");
}
public static void main(String[] args) {
Demo d1 = new Demo();
d1.second();
}
}
First method
Second method
The call this.first() invokes the
first() method on the current object.
In this example, simply writing first()
would also work.
The expression this() can call another
constructor in the same class. This is called
constructor chaining.
class Student {
String name;
int age;
Student() {
this("Unknown", 0);
}
Student(String name, int age) {
this.name = name;
this.age = age;
}
void display() {
System.out.println(name + " " + age);
}
public static void main(String[] args) {
Student s1 = new Student();
s1.display();
}
}
Unknown 0
The no-argument constructor delegates initialization to the two-parameter constructor.
this(...)
must appear first in the constructor body, subject to
the flexible constructor body rules introduced in
newer Java versions.
An instance method can return this.
This is useful when creating methods that can be chained.
class Student {
String name;
Student setName(String name) {
this.name = name;
return this;
}
void display() {
System.out.println(name);
}
public static void main(String[] args) {
Student s1 = new Student();
s1.setName("Rahul").display();
}
}
Rahul
The setName() method returns the same
object, allowing display() to be called
immediately afterward.
The current object can be passed as an argument to another method.
class Student {
String name = "Amit";
void display() {
System.out.println(name);
}
void sendTo(Printer printer) {
printer.printStudent(this);
}
}
class Printer {
void printStudent(Student student) {
student.display();
}
}
class Main {
public static void main(String[] args) {
Student s1 = new Student();
Printer p1 = new Printer();
s1.sendTo(p1);
}
}
Amit
Here, this passes the current Student
object to the Printer object's method.
Each object has its own current-object reference when its instance methods execute.
class Student {
String name;
Student(String name) {
this.name = name;
}
void display() {
System.out.println(this.name);
}
public static void main(String[] args) {
Student s1 = new Student("Rahul");
Student s2 = new Student("Priya");
s1.display();
s2.display();
}
}
Rahul
Priya
When s1.display() executes,
this refers to the object referenced by
s1. When s2.display()
executes, it refers to the object referenced by
s2.
The this keyword cannot be used directly
inside a static method because a static method is
associated with the class, not a particular object.
class Demo {
void display() {
System.out.println("Instance method");
}
static void show() {
System.out.println("Static method");
// this.display(); // Compilation error
}
public static void main(String[] args) {
Demo.show();
Demo d1 = new Demo();
d1.display();
}
}
Static method
Instance method
this in a static context.
Create or receive an object reference if you need
to access an instance method from a static method.
| this | super |
|---|---|
| Refers to the current object. | Refers to the superclass portion of the current object. |
| Can access current-class instance members. | Can access accessible superclass members. |
this() calls another constructor in the same class. |
super() calls a superclass constructor. |
class Animal {
String name = "Animal";
}
class Dog extends Animal {
String name = "Dog";
void display() {
System.out.println(this.name);
System.out.println(super.name);
}
public static void main(String[] args) {
Dog d1 = new Dog();
d1.display();
}
}
Dog
Animal
Mistake 1: Using this in a static method
static void show() {
// this.name = "Rahul"; // Compilation error
}
Mistake 2: Confusing a parameter with a field
class Student {
String name;
Student(String name) {
this.name = name;
}
}
Using this.name makes it clear that the
field is being assigned the parameter's value.
Mistake 3: Calling this() incorrectly
Student() {
// System.out.println("Start");
// this("Rahul"); // Invalid placement in traditional syntax
}
Student(String name) {
this.name = name;
}
A constructor delegation call must follow the applicable constructor-body rules.
class Employee {
String name;
double salary;
Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
Employee updateSalary(double salary) {
this.salary = salary;
return this;
}
void display() {
System.out.println("Name: " + this.name);
System.out.println("Salary: " + this.salary);
}
public static void main(String[] args) {
Employee e1 = new Employee("Amit", 25000);
e1.updateSalary(30000).display();
}
}
Name: Amit
Salary: 30000.0
This example uses this to assign fields
and return the current object for method chaining.
this to assign the name and age
constructor parameters to instance fields.
this.
this() to call one constructor from
another.
this.
this and
super.
this
identifies that object.
1. What does this refer to in an instance method?
2. Which expression calls another constructor in the same class?
3. Can this be used directly inside a static method?