this is a reference to the current object.
It is used when we want to access the current object's fields, methods, or constructors.
Most commonly, this is used when a parameter and a field have the same name.
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
}
Here:
this.name → object's field
name → constructor parameter
So:
this.name = name;
means assign the parameter name to the current object's name field.
class Student {
void study() {
System.out.println("Studying");
}
void start() {
this.study();
}
}
this.study() means:
Call
study()on the current object.
this can often be omitted here:
study();
this can be passed as an argument to another method or constructor.