← Back to Core Java Course | Chapter 7: OOP Core | Lesson 4 of 11

Java this Keyword

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

java
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);
	}
}

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

java
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());
	}
}

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

java
class Greeter {
	void greet() {
		this.sayHello();
	}
	void sayHello() {
		System.out.println("Hello!");
	}
}
public class Main {
	public static void main(String[] args) {
		new Greeter().greet();
	}
}

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

java
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);
	}
}

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

java
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 run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.