Java this Keyword
In this page:
this.field = parameter;
this.methodName();
this(arguments); // call another constructor
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
// Define the class Logger
class Logger {
void register(Registry r) {
r.add(this);
}
}
// Define the class Registry
class Registry {
void add(Logger l) {
// Print a line to the console
System.out.println("Registered: " + l);
}
}
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Logger object and store it in logger
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
// Define the class Greeter
class Greeter {
void greet() {
this.sayHello();
}
void sayHello() {
// Print a line to the console
System.out.println("Hello!");
}
}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
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
// Define the class Box
class Box {
int size;
Box() {
this(10);
}
Box(int size) {
this.size = size;
}
}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Box object and store it in b
Box b = new Box();
// Print a line to the console
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
// Define the class Builder
class Builder {
// Create a new StringBuilder object and store it in sb
StringBuilder sb = new StringBuilder();
Builder add(String s) {
sb.append(s);
// Send this back to the caller
return this;
}
}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new Builder object and store it in b
Builder b = new Builder();
b.add("Hello").add(" World");
// Print a line to the console
System.out.println(b.sb);
}
}
Login to try C/C++/Java/PHP code in the editor
- Using
thisinside a static method, which is a compile error. - Writing
name = namein a constructor, which assigns the parameter to itself and leaves the field unchanged; usethis.name = name. - Calling
this()from a normal method, since it only works as the first statement of a constructor.
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: