The this Keyword in Java
Inside an instance method, this is a reference to the object the method was called on, which resolves shadowing and enables chaining.
-
Java Basics
- Introduction to Java
- Setting Up Java and Writing Your First Program
- Variables, Data Types and Literals in Java
- Type Casting and Type Conversion in Java
- Operators and Expressions in Java
- Input and Output in Java
- Comments, Keywords and Naming Conventions in Java
- Control Flow in Java: if, else and switch
- Loops in Java: for, while and do-while
- Methods
- Arrays and Strings
-
OOP
- Classes and Objects in Java
- Constructors in Java
- The this Keyword in Java
- The static Keyword in Java
- Encapsulation in Java
- Access Modifiers in Java
- Inheritance in Java
- Method Overriding and super in Java
- Polymorphism in Java
- Abstraction, Abstract Classes and Interfaces in Java
- Composition, Aggregation and Association in Java
- The Object Lifecycle in Java
- Core Java
- Exception Handling
-
Collections
- The Java Collections Framework
- List in Java: ArrayList, LinkedList, Vector and Stack
- Set in Java: HashSet, LinkedHashSet and TreeSet
- Map in Java: HashMap, LinkedHashMap and TreeMap
- How HashMap Works Internally in Java
- Queue and Deque in Java: ArrayDeque and PriorityQueue
- Iterators in Java
- Comparable and Comparator in Java
- Collections Utilities and Choosing the Right Collection
- Generics
- Java 8+
- Stream API
- Date and Time
- File and I/O
-
Multithreading
- Threads in Java: Processes, Runnable and Thread
- Thread Lifecycle in Java
- Synchronization in Java: synchronized and volatile
- Locks and Atomic Classes in Java
- Race Conditions and Deadlocks in Java
- The Executor Framework and Thread Pools in Java
- Future and CompletableFuture in Java
- Concurrent Collections in Java
- The Java Memory Model
- JVM and Memory
- Advanced Java
- Networking
- JDBC
- Testing
Definition
this is a reference to the current object. It is available in every instance method, constructor and instance initialiser block, and it is not available in a static context because there is no current object there.
Its four uses
1. Distinguishing a field from a parameter
public class Product {
private String name;
private double price;
public Product(String name, double price) {
this.name = name; // field = parameter
this.price = price;
}
}The parameter shadows the field, so a plain name refers to the parameter. Without this, name = name; assigns the parameter to itself and the field stays null, which compiles cleanly and fails silently.
2. Calling another constructor
public class Order {
private final String reference;
private final int quantity;
public Order(String reference) {
this(reference, 1); // must be the first statement
}
public Order(String reference, int quantity) {
this.reference = reference;
this.quantity = quantity;
}
}3. Returning the current object for chaining
public class QueryBuilder {
private final StringBuilder sql = new StringBuilder("SELECT * FROM notes");
public QueryBuilder where(String condition) {
sql.append(" WHERE ").append(condition);
return this;
}
public QueryBuilder orderBy(String column) {
sql.append(" ORDER BY ").append(column);
return this;
}
public String build() {
return sql.toString();
}
}String query = new QueryBuilder()
.where("status = 1")
.orderBy("created_at")
.build();4. Passing the current object to something else
public class Order {
public void submit(OrderValidator validator) {
validator.check(this); // hand the whole object over
}
}Explicit method calls
public class Report {
public void print() {
this.render(); // identical to render()
render();
}
private void render() { }
}this before a method call is optional and adds nothing. Some teams use it for emphasis; most omit it.
Rules
thiscannot be used inside astaticmethod or a static initialiser.this(...)andsuper(...)cannot both appear in one constructor, and either must be the first statement.thiscannot be reassigned; it is effectively final.- In an inner class,
thismeans the inner instance. UseOuter.thisto reach the enclosing one.
public class Outer {
private String label = "outer";
class Inner {
private String label = "inner";
void show() {
System.out.println(this.label); // inner
System.out.println(Outer.this.label); // outer
}
}
}this in lambdas
public class Service {
private final String name = "service";
public Runnable asLambda() {
return () -> System.out.println(this.name); // the Service instance
}
public Runnable asAnonymousClass() {
return new Runnable() {
@Override
public void run() {
// this here would be the Runnable, not the Service
System.out.println(Service.this.name);
}
};
}
}A lambda does not introduce a new this. It sees the enclosing instance directly, which is one of the practical differences between a lambda and an anonymous class.Common mistakes
- Writing
name = name;in a constructor and leaving the field null. - Trying to use
thisinmainor any other static method. - Placing
this(...)anywhere but the first line of a constructor. - Leaking
thisfrom a constructor by registering the half built object with a listener or a collection.
Best practices
- Use
this.field = parameterin constructors and setters, and give the parameter the same name as the field. - Return
thisfrom mutating methods only when a fluent API is genuinely intended. - Do not publish
thisbefore the constructor finishes. - Skip
thisbefore ordinary method calls unless the codebase has a standing convention.
Practice
- Remove
thisfrom a constructor that assigns same named parameters and explain what the object looks like afterwards. - Write a small fluent builder with three chained methods.
- Why is
thisunavailable inpublic static void main? - In a nested class, print both the inner and the outer field of the same name.
- Explain what a lambda sees when it refers to
this, and how that differs from an anonymous class.
Conclusion
Reach for this when a name is shadowed, when one constructor should delegate to another, when a method should return the object for chaining, or when the object itself has to be handed to someone else.