Menu
Coddy logo textTech

The this Keyword

Part of the Object Oriented Programming section of Coddy's Java journey — lesson 4 of 87.

The this keyword refers to the current instance of a class. It distinguishes between fields and parameters with the same name.

Without this, the parameter shadows the field

public class Person {
    private String name;
    
    public Person(String name) {
        name = name;  // Assigns parameter to itself!
    }
}

Using this to correctly assign values

public class Person {
    private String name;
    
    public Person(String name) {
        this.name = name;  // Assigns parameter to field
    }
    
    public String getName() {
        return this.name;
    }
}

Using this in methods

public class Person {
    private String name;
    private int age;
    
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    public String getDescription() {
        return this.name + " is " + this.age + " years old";
    }
}

The this keyword always points to the current object. It's essential when parameter names match field names, preventing confusion and bugs.

challenge icon

Challenge

Medium

Complete the Person class using the this keyword to distinguish between fields and parameters:

  • A constructor that uses this to assign parameters to fields
  • Getter methods using this to access fields
  • A getDescription() method returning "<name>, age <age>, from <city>"

Cheat sheet

The this keyword refers to the current instance of a class and distinguishes between fields and parameters with the same name.

Without this, the parameter shadows the field:

public Person(String name) {
    name = name;  // Assigns parameter to itself!
}

Using this to correctly assign values:

public Person(String name) {
    this.name = name;  // Assigns parameter to field
}

Using this in getter methods:

public String getName() {
    return this.name;
}

Using this with multiple fields:

public class Person {
    private String name;
    private int age;
    
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    public String getDescription() {
        return this.name + " is " + this.age + " years old";
    }
}

Try it yourself

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int age = Integer.parseInt(scanner.nextLine());
        String city = scanner.nextLine();
        
        Person person = new Person(name, age, city);
        
        System.out.println("Name: " + person.getName());
        System.out.println("Age: " + person.getAge());
        System.out.println("City: " + person.getCity());
        System.out.println(person.getDescription());
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming