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

Java this Keyword

this means 'the object I am inside right now', like saying me or my. It helps when a name could mean two things and lets an object talk about itself.
Syntax
java
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

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

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

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

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
// 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);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using this inside a static method, which is a compile error.
  2. Writing name = name in a constructor, which assigns the parameter to itself and leaves the field unchanged; use this.name = name.
  3. Calling this() from a normal method, since it only works as the first statement of a constructor.

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.