Skip to content

The this Keyword

Introduced
Handout
Video lesson for this topic Open the video page
7:25

Class Creation

Here is a student, stored as three loose variables: a name, a year, and a grade. That works. Now add a thousand students. You need three thousand variables…

English narration · English and 中文 subtitles you can switch

In this lesson

Video lesson

A parameter can hide the field it should update

  • An object has name "Ada". setName(String name) runs name = name; with the argument "Bo".
  • Both unqualified names refer to the parameter, so the object still has "Ada". this.name = name assigns the parameter value to the field instead.
A parameter can hide the field it should update: in setName(String name), name = name assigns the parameter to itself, so the object keeps "Ada"; this.name = name assigns the parameter value "Bo" to the field

Handout

Log in or create account

IGCSE, A-Level & AP