The this Keyword

IB Syllabus: B3.1 – Use the this keyword to refer to the current object.

Table of Contents

  1. Key Concepts
    1. What is this?
    2. The Parameter Shadowing Problem
    3. The Fix: this.variable
    4. When this is Required
    5. When this is Optional
    6. Using this in Setters
    7. Avoiding the Problem Entirely
  2. Worked Examples
    1. Example 1: Tracing this vs No this
    2. Example 2: Full Class Using this Throughout
  3. Quick Check
  4. Spot the Error
  5. Predict the Output
  6. Practice Exercises
    1. Core
    2. Extension
    3. Challenge
  7. Connections

Key Concepts

What is this?

The this keyword is a reference to the current object – the specific instance whose method or constructor is currently executing. Every non-static method and constructor has an invisible this reference that points to the object the code is running on.

You use this most often to resolve name conflicts between method parameters and instance variables.

The Parameter Shadowing Problem

When a constructor or method parameter has the same name as an instance variable, the parameter shadows (hides) the instance variable. Without this, the compiler uses the closest scope – the parameter – for both sides of the assignment.

public class Pet {
    private String name;

    public Pet(String name) {
        name = name;  // BUG: assigns the parameter to itself
                      // the instance variable stays null
    }

    public String getName() { return name; }
}
Pet p = new Pet("Max");
System.out.println(p.getName());  // null -- the instance variable was never set!

The line name = name assigns the parameter name to itself. The instance variable this.name is never touched and remains null.

The Fix: this.variable

Using this.name explicitly refers to the instance variable, while the bare name refers to the parameter:

public class Pet {
    private String name;

    public Pet(String name) {
        this.name = name;  // this.name = instance variable
                           // name = parameter
    }

    public String getName() { return name; }
}
Pet p = new Pet("Max");
System.out.println(p.getName());  // Max -- works correctly

this.name always refers to the instance variable. Plain name refers to the closest scope – inside a constructor or method with a name parameter, that means the parameter. Outside such a context, plain name and this.name are identical.

When this is Required

this is required when a parameter name matches an instance variable name. Without it, the assignment does nothing useful.

public void setAge(int age) {
    this.age = age;  // required -- parameter shadows instance variable
}

When this is Optional

If there is no name conflict, this is optional. Both versions below are equivalent:

public double getArea() {
    return this.width * this.height;  // explicit -- works
}

public double getArea() {
    return width * height;            // implicit -- also works (no shadowing)
}

Many programmers use this consistently even when optional, for clarity. Others only use it when needed. Both styles are accepted.

Using this in Setters

Setters almost always have a parameter with the same name as the attribute they modify:

public class Circle {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return radius;  // no conflict, this is optional
    }

    public void setRadius(double radius) {
        if (radius > 0) {
            this.radius = radius;  // required -- parameter shadows field
        }
    }

    public double getArea() {
        return Math.PI * radius * radius;
    }
}

Avoiding the Problem Entirely

Some programmers avoid shadowing by giving parameters different names:

public Pet(String petName) {
    name = petName;  // no conflict, no this needed
}

This works, but the IB convention and most Java style guides prefer matching names with this:

public Pet(String name) {
    this.name = name;  // standard Java convention
}

The this approach is what you will see in IB exam mark schemes and textbooks.


Worked Examples

Example 1: Tracing this vs No this

With this:

public class Point {
    private int x;
    private int y;

    public Point(int x, int y) {
        this.x = x;  // instance variable = parameter
        this.y = y;
    }
}

Point p = new Point(3, 7);
// p.x = 3, p.y = 7 ✓

Without this:

public class Point {
    private int x;
    private int y;

    public Point(int x, int y) {
        x = x;  // parameter = parameter (no effect on instance variable)
        y = y;
    }
}

Point p = new Point(3, 7);
// p.x = 0, p.y = 0 ✗ (instance variables keep default int value)

Example 2: Full Class Using this Throughout

public class Employee {
    private String name;
    private double salary;
    private String department;

    public Employee(String name, double salary, String department) {
        this.name = name;
        this.salary = salary;
        this.department = department;
    }

    public String getName() { return name; }
    public double getSalary() { return salary; }
    public String getDepartment() { return department; }

    public void setSalary(double salary) {
        if (salary >= 0) {
            this.salary = salary;
        }
    }

    public void raiseSalary(double percentage) {
        // no conflict here -- percentage is not an instance variable name
        salary = salary * (1 + percentage / 100.0);
    }
}

Notice: raiseSalary does not need this because percentage does not shadow any instance variable. setSalary does need it because the parameter salary shadows the field salary.


Quick Check

Q1. What does this refer to in Java?

Q2. If a constructor has String name as a parameter and the class has a private String name field, what does name = name (without this) do?

Q3. When is this required?


Spot the Error

This setter compiles but has no effect -- calling setRadius(5.0) does not change the circle's radius. Click the buggy line, then pick the fix.

1public class Circle { 2 private double radius; 3 public Circle(double radius) { this.radius = radius; } 4 public void setRadius(double radius) { 5 radius = radius; 6 } 7}

Pick the fix:


Predict the Output

What does this print?

public class Pet {
    private String name;
    public Pet(String name) { this.name = name; }
    public String getName() { return name; }
}

Pet p = new Pet("Luna");
System.out.println(p.getName());

What does this print? (Notice: no this in the constructor)

public class Pet {
    private String name;
    public Pet(String name) { name = name; }
    public String getName() { return name; }
}

Pet p = new Pet("Luna");
System.out.println(p.getName());

Practice Exercises

Core

  1. Fix the bug – The following constructor has a shadowing bug. Rewrite it correctly:
    public class Car {
        private String make;
        private int year;
        public Car(String make, int year) {
            make = make;
            year = year;
        }
    }
    
  2. Identify where this is needed – In the following class, mark which lines need this and which do not. Explain why.
    public class Timer {
        private int seconds;
        public Timer(int seconds) { seconds = seconds; }
        public int getSeconds() { return seconds; }
        public void addTime(int extra) { seconds = seconds + extra; }
        public void reset(int seconds) { seconds = seconds; }
    }
    

Extension

  1. Full class with this – Write a Movie class with attributes title, director, and rating (double, 0-10). Write a parameterised constructor using this, getters, and a setter setRating(double rating) that validates the range. All parameter names should match the attribute names.

Challenge

  1. Trace and explain – A class has this code:
    public class Pair {
        private int a;
        private int b;
        public Pair(int a, int b) { this.a = a; this.b = b; }
        public void swap() {
            int temp = a;
            a = b;
            b = temp;
        }
    }
    

    Trace Pair p = new Pair(3, 7); p.swap(); and state the final values of a and b. Does swap() need this? Why or why not?


Connections

  • Prerequisites: Constructorsthis is most commonly used inside constructors
  • Prerequisites: Classes and Objects – understanding instance variables and methods
  • Next: Encapsulation – this enables the getter/setter pattern that encapsulation relies on
  • Related: Inheritance – this refers to the current object even in inherited methods; super refers to the parent

Back to top

© EduCS.me — A resource hub for IB Computer Science

This site uses Just the Docs, a documentation theme for Jekyll.