Java this Keyword
In this page:
Resolving Shadowed Instance Fields
When a constructor or method parameter shares a name with an instance field, the parameter locally shadows the field inside that block. Writing this.name = name; disambiguates the two, explicitly assigning the parameter's value to the object's field rather than to itself.
Example: Resolving Shadowed Instance Fields
class Person {
String name;
Person(String name) {
this.name = name; // disambiguates field from parameter
}
}
public class Main {
public static void main(String[] args) {
Person p = new Person("Alice");
System.out.println(p.name);
}
}
Login to try C/C++/Java/PHP code in the editor
Passing 'this' as an Argument
You can pass the current object as an argument to another method using this, which is handy when an object needs to register or hand off a reference to itself, such as adding itself to a listener list.
Example: Passing 'this' as an Argument
class Logger {
void register(Registry r) {
r.add(this);
}
}
class Registry {
void add(Logger l) {
System.out.println("Registered: " + l);
}
}
public class Main {
public static void main(String[] args) {
Logger logger = new Logger();
logger.register(new Registry());
}
}
Login to try C/C++/Java/PHP code in the editor
Invoking Instance Methods
Calling this.someMethod() inside another instance method explicitly invokes it on the current object; it's optional since Java implies this automatically, but writing it out can make it visually clear the call targets a local instance method rather than a static or external one.
Example: Invoking Instance Methods
class Greeter {
void greet() {
this.sayHello();
}
void sayHello() {
System.out.println("Hello!");
}
}
public class Main {
public static void main(String[] args) {
new Greeter().greet();
}
}
Login to try C/C++/Java/PHP code in the editor
Constructor Delegation Chaining
this(...) invoked as the first line of a constructor delegates to a different overloaded constructor in the same class, letting multiple constructors share one common initialization path instead of repeating setup code in each.
Example: Constructor Delegation Chaining
class Box {
int size;
Box() {
this(10);
}
Box(int size) {
this.size = size;
}
}
public class Main {
public static void main(String[] args) {
Box b = new Box();
System.out.println(b.size);
}
}
Login to try C/C++/Java/PHP code in the editor
Returning 'this' for Method Chaining
Returning this from an instance method hands back a reference to the current object, which is what enables method chaining — calling several methods back-to-back in one fluent statement, like builder.setName("x").setAge(20).build().
Example: Returning 'this' for Method Chaining
class Builder {
StringBuilder sb = new StringBuilder();
Builder add(String s) {
sb.append(s);
return this;
}
}
public class Main {
public static void main(String[] args) {
Builder b = new Builder();
b.add("Hello").add(" World");
System.out.println(b.sb);
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: