New to Rust? Grab our free Rust for Beginners eBook Get it free →
Java this Keyword: Uses and Examples

A Java parameter can have the same name as a field. The expression this.name refers to the field on the current object.
I’ll show how this assigns a parameter to an object’s field and how to tell a constructor call from a field access.
TL;DR: Java this refers to the current object
In an instance method or constructor, Java’s this reference identifies the object whose code is running. Use it to distinguish an instance field from a same-named parameter, or use this() to delegate from one constructor to another.
- this.field selects a field on the current object.
- this() calls another constructor in the same class.
- A static method has no current object, so it cannot use this.
What is the this keyword in Java?
The keyword refers to the current object, meaning the particular class instance whose constructor or instance method is executing.
Parameters hold inputs for one method or constructor call. A same-named parameter shadows the field, so writing this.name = name assigns the input to the current object’s field.
I use this naming in the example because it keeps the field and its incoming value distinct without requiring two different names. The left side identifies stored state and the right side supplies the value.
How to use this in Java step by step
The example combines the two common tasks: a no-argument constructor delegates to a second constructor, which assigns its parameter to the object’s field. I kept both behaviors in one small class so the output reflects the same object moving through both constructors.
Step 1: Assign a parameter to an instance field
The one-argument constructor stores its input in the field. The no-argument constructor calls it with this(“Ada”), so the delegated constructor performs the assignment.
class ThisKeywordDemo {
private final String name;
ThisKeywordDemo() {
this("Ada");
}
ThisKeywordDemo(String name) {
this.name = name;
}
String name() {
return this.name;
}
public static void main(String[] args) {
ThisKeywordDemo person = new ThisKeywordDemo();
System.out.println(person.name());
}
}
I compiled and ran ThisKeywordDemo.java with javac ThisKeywordDemo.java && java ThisKeywordDemo. It printed:
Ada

The output is Ada, confirming that the object returned the field value initialized by the constructor.
Step 2: Read the constructor call correctly
this(“Ada”) is a constructor invocation, while this.name is a field access. A dot followed by a member name selects a field or method on the current object.
Put a constructor delegation call first in its body. Avoid circular delegation, where overloaded constructors eventually call one another again.
Edge cases: When can you not use this in Java?
You cannot refer to a current instance from a static method because static code belongs to the class rather than a particular object. Use an object reference to access instance behavior, or move the operation into an instance method when it depends on that object.
Static code can still use static members without an object receiver. When logic needs an instance field, pass the object explicitly or make the operation an instance method so Java has a receiver.
| Use | Meaning | Constraint |
|---|---|---|
| this.field | Read or assign a field on this instance | Needs an instance context |
| this.method() | Call an instance method on this object | The prefix is optional when no name is ambiguous |
| this() | Delegate to another constructor in this class | Must be the first constructor statement |
| Pass or return this | Share or return the current object reference | Use only when exposing that object is intended |
The prefix on this.method() is optional because an unqualified instance-method call already uses the current object. Keep it when the receiver is useful to make explicit, and otherwise follow the naming style used by the class.
Passing or returning plain this lets other code work with the same instance. It does not copy the object’s fields, so a change through either reference is visible through both. I keep this choice for APIs where callers should share that instance.
Conclusion: Use this to name the current object
Java’s this names the receiver rather than the class. Keeping that distinction in mind makes instance behavior clearer.
For the formal definition and constructor examples, see Oracle’s Using the this Keyword tutorial and the Java Language Specification, Classes. Browse the site’s Java articles for another topic, or continue to its Java and MySQL JDBC tutorial when you are ready to connect an application to a database.
FAQ
These answers cover when the reference is required and which context can use it.
Is the this keyword required in Java?
No. Use it when a parameter shadows an instance field, when explicit receiver notation helps, or to invoke another constructor with this(…). Unambiguous instance members can be accessed without it.
Can you use this in a static method?
No. A static method has no current object. Use an instance reference or place object-dependent work in an instance method.
What does this() mean in a constructor?
It invokes another constructor in the same class. If present, it must be the first statement in that constructor.


