Module 5: Classes, Objects, and Constructors
Object State and the `this` Keyword
Use `this` to refer to the current object and make constructor or method code easier to read.
Author
Java Learner Editorial Team
Reviewer
Technical review by Java Learner
Last reviewed
2026-04-16
Java version
Java 25 LTS
Learning goals
- Understand what `this` refers to
- Use `this` inside constructors and methods
- Disambiguate between fields and parameters with the same name
this means “this current object”: It lets a method or constructor refer to the object that is currently being worked on.
It is especially common in constructors: When a parameter and a field have the same name, this.name = name; makes the difference clear.
this also makes object-focused thinking easier: It reminds you that methods work with the state of the current object.
Beginner rule: Use this when it improves clarity, especially for field assignment.
Runnable examples
Use `this` to separate fields from parameters
class Student {
String name;
Student(String name) {
this.name = name;
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student("Sara");
System.out.println(student.name);
}
}Expected output
Sara
Common mistakes
Writing `name = name;` and expecting the field to change
Use `this.name = name;` so Java knows the field on the left and the parameter on the right.
Mini exercise
Add a constructor to a `Laptop` class that uses `this.brand = brand;` and `this.price = price;`.
Summary
- `this` refers to the current object.
- It is common in constructors and setters.
- It helps when field names and parameter names match.
Next step
Finish the module by protecting object data with encapsulation.
Sources used