this Keyword
Referring to current object
5 min read
this Keyword in Java
The this keyword refers to the current object - the instance on which a method or constructor is being called.
Uses of this Keyword
- this.field: Distinguish instance variable from parameter
- this.method(): Call another method of current object
- this(): Call another constructor (constructor chaining)
- return this: Return current object (method chaining)
- this as argument: Pass current object to another method
⚠️ Important: this cannot be used in static methods because static methods belong to the class, not an instance!
💡 Tip: Using this explicitly can make code clearer, even when not strictly required.
Code Examples
Using this to distinguish fields from parameters
java
1// this.field - Distinguishing instance variable from parameter
2public class Person {
3 private String name;
4 private int age;
5
6 // Without 'this' - WRONG! Parameter shadows field
7 public void setNameBad(String name) {
8 name = name; // Does nothing! Assigns parameter to itself
9 }
10
11 // With 'this' - CORRECT!
12 public void setName(String name) {
13 this.name = name; // Assigns parameter to instance field
14 }
15
16 public void setAge(int age) {
17 this.age = age;
18 }
19
20 // Constructor using this
21 public Person(String name, int age) {
22 this.name = name;
23 this.age = age;
24 }
25}Constructor chaining with this()
java
1// this() - Constructor chaining
2public class Book {
3 private String title;
4 private String author;
5 private int pages;
6
7 // Full constructor
8 public Book(String title, String author, int pages) {
9 this.title = title;
10 this.author = author;
11 this.pages = pages;
12 }
13
14 // Calls full constructor with default pages
15 public Book(String title, String author) {
16 this(title, author, 0); // this() must be first statement!
17 }
18
19 // Calls two-arg constructor with default author
20 public Book(String title) {
21 this(title, "Unknown");
22 }
23
24 // Calls one-arg constructor with default title
25 public Book() {
26 this("Untitled");
27 }
28}
29
30// All these create valid Book objects
31Book b1 = new Book();
32Book b2 = new Book("Java 101");
33Book b3 = new Book("Java 101", "John Doe");
34Book b4 = new Book("Java 101", "John Doe", 500);Method chaining by returning this
java
1// return this - Method chaining (Builder/Fluent pattern)
2public class StringBuilder2 {
3 private String value = "";
4
5 public StringBuilder2 append(String s) {
6 value += s;
7 return this; // Return current object for chaining
8 }
9
10 public StringBuilder2 appendLine(String s) {
11 value += s + "\n";
12 return this;
13 }
14
15 public StringBuilder2 clear() {
16 value = "";
17 return this;
18 }
19
20 public String build() {
21 return value;
22 }
23}
24
25// Fluent/chained method calls
26String result = new StringBuilder2()
27 .append("Hello")
28 .append(" ")
29 .append("World")
30 .appendLine("!")
31 .append("Welcome")
32 .build();
33
34System.out.println(result);
35// Output:
36// Hello World!
37// WelcomePassing this as a method argument
java
1// this as method argument
2public class EventHandler {
3 private String name;
4
5 public EventHandler(String name) {
6 this.name = name;
7 }
8
9 public void registerWith(EventManager manager) {
10 manager.addHandler(this); // Pass current object
11 }
12
13 public void handle(String event) {
14 System.out.println(name + " handling: " + event);
15 }
16}
17
18public class EventManager {
19 private List<EventHandler> handlers = new ArrayList<>();
20
21 public void addHandler(EventHandler handler) {
22 handlers.add(handler);
23 }
24
25 public void fire(String event) {
26 for (EventHandler h : handlers) {
27 h.handle(event);
28 }
29 }
30}
31
32// Usage
33EventManager manager = new EventManager();
34EventHandler h1 = new EventHandler("Handler1");
35EventHandler h2 = new EventHandler("Handler2");
36
37h1.registerWith(manager); // Passes 'this'
38h2.registerWith(manager);
39
40manager.fire("click");
41// Handler1 handling: click
42// Handler2 handling: clickUse Cases
- Distinguishing fields from parameters
- Constructor chaining
- Method chaining (fluent APIs)
- Passing current object to other methods
- Returning current object from methods
- Self-referencing in callbacks
Common Mistakes to Avoid
- Using this in static context
- Not using this when needed (shadowing)
- this() not as first statement
- Calling this() in a method (only constructors)
- Confusing this with the class
- Unnecessary use making code verbose