Java Notes
Understand the this keyword in Java — what it refers to, how it resolves field vs parameter name clashes, constructor chaining with this(), and passing the current object to another method.
this is a reference that always means the current object — the one whose method or constructor is running right now.
Java uses it in four main situations. Each one solves a real problem. Understanding each situation separately makes this completely clear.
Situation 1: Resolving Field vs Parameter Name Clash
The most common use. When a constructor or method parameter has the same name as a field, Java cannot tell which is which. this.fieldName always refers to the object's field.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
this.name = name;— left sidethis.nameis the field on the current object. Right sidenameis the constructor parameter.- Without
this., Java seesname = name— the parameter assigning to itself. The field stays at its default value (null). - Inside
print(), there is no clash — you could write justnamewithoutthis. Writingthis.nameis also fine and some developers prefer it for clarity.
Run this and verify the name prints correctly. Then remove this. from the constructor line and run again — the name will print null.
Situation 2: Calling One Constructor From Another — this()
this() calls another constructor in the same class. This avoids repeating initialization code when you have multiple constructors.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
this(name, department, 30000.0)— calls the three-argument constructor. All actual field assignments happen there.this()must always be the first statement in a constructor. You cannot put any other statement before it.- The chain:
Employee("Priya")→Employee("Priya", "General")→Employee("Priya", "General", 30000.0).
This way, initialization logic lives in one place — the full constructor. The others just provide default values.
Situation 3: Passing the Current Object to Another Method
A method can pass itself (this) as an argument to another method or class. This is useful when two objects need to interact.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
printer.print(this)—thisis the currentStudentobject (the one that calledsendToPrinter()).- The
Printerreceives the object and accesses itsnamefield.
This pattern appears frequently when one class needs to hand itself off to another for processing.
Situation 4: Returning the Current Object
A method can return this to allow method chaining — calling multiple methods on the same object in one line.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
- Each method sets a field and returns
this— the sameQueryBuilderobject. .from(...).where(...).limit(...).build()— each call happens on the same object. This is called a fluent interface.- The final
build()does not returnthisbecause the chain ends there.
This style is used heavily in libraries like StringBuilder, stream builders, and test assertion frameworks.
What this Cannot Do
this only works in instance methods and constructors. It does not exist in static methods.
A static method belongs to the class, not to any object. Since there is no object, there is no this.
Common Mistake 1: Forgetting this. in Constructor
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Common Mistake 2: this() Not as First Statement
// WRONG
public Employee(String name) {
System.out.println("Creating..."); // statement before this()
this(name, "General"); // COMPILE ERROR
}
// CORRECT
public Employee(String name) {
this(name, "General"); // must be first
System.out.println("Creating...");
}Common Mistake 3: Using this in Static Method
// WRONG
public static void showName() {
System.out.println(this.name); // COMPILE ERROR
}
// CORRECT — make method non-static or pass the object as parameter
public void showName() {
System.out.println(this.name);
}Practice Task
Task: Create a class Counter with a field count (int, starts at 0). Write three methods:
increment()— increases count by 1, returnsthisadd(int n)— increases count by n, returnsthisreset()— sets count back to 0, returnsthisprint()— prints the current count (no return needed)
In main, use method chaining: new Counter().add(5).increment().increment().add(3).print().
Hint: Each method that returns this must declare the return type as Counter. The last method print() can return void.
Solution:
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
What to observe: Try storing the result of new Counter() in a variable. Call methods with and without chaining. You will see both produce the same result — chaining is just a style choice that reduces lines of code.
Continue the lesson path