Le mot-clé this
IntroduitA 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.

"This object"
- Inside an instance method,
thisrefers to the object the method was called on.student.addScore(10)runs withthis= thatstudent. this.scoremeans "thescoreof the object I'm working on." It lets a method talk about its own object explicitly.
Distinguishing field from parameter
- The main use: tell an instance variable apart from a parameter of the same name.
void setName(String name) { this.name = name; } this.nameis the instance variable; plainnameis the parameter. This is the standard fix for the shadowing bug from 3.8.
Access to the receiver object
thisgives a method a handle on the exact object it was called on. Every instance method has an implicitthis, whether you write it or not.- When score names an unshadowed instance field,
score += s;usesthis.score. If a parameter or local named score shadows it, the unqualified name instead refers to that variable. You writethisexplicitly only when needed (like resolving shadowing).
this désigne l'objet récepteur
Dans setName, this est l'objet ada sur lequel il a été appelé.
Dans une méthode d'instance, this fait référence à...
Difficulté 1/5 · 100 pièces + un petit bonus lors de la première réussite aujourd'hui
this est une poignée sur l'objet actuel.
Dans setName(String name) { this.name = name; }, this.name est...
Difficulté 1/5 · 100 pièces + un petit bonus lors de la première réussite aujourd'hui
this.name est le champ ; name simple est le paramètre.
Le mot-clé this peut être utilisé à l'intérieur d'une méthode statique.
Difficulté 1/5 · 100 pièces + un petit bonus lors de la première réussite aujourd'hui
Les méthodes statiques n'ont pas d'objet particulier, donc pas de this.
Écrire name = name; (sans this) à l'intérieur de setName(String name) fait quoi ?
Difficulté 1/5 · 100 pièces + un petit bonus lors de la première réussite aujourd'hui
Les deux noms sont le paramètre ; utilisez this.name pour accéder au champ.
Chaque méthode d'instance a un this implicite, que vous l'écriviez ou non.
Difficulté 1/5 · 100 pièces + un petit bonus lors de la première réussite aujourd'hui
Une méthode d'instance possède un récepteur courant. Un score non qualifié nomme ce.score uniquement si aucun paramètre ou variable locale ne masque ce champ.
No this in a static method
- A static method has no
this— it isn't tied to any particular object. So a static method cannot usethisor an unqualified instance field, but may read an instance field through an explicit reference when access permission allows it. - Trying to use
thisin a static method is a compile error. If you needthis, the method must be an instance method, not static.
Use this.field = param; to defeat shadowing — this.name is the instance variable, plain name is the parameter. Writing name = name; (no this) just assigns the parameter to itself. And this doesn't exist in a static method — a static method has no particular object, so using this there won't compile.
Type the field value after the faulty setName("Bo") call on the described object.
Difficulté 1/5 · 100 pièces + un petit bonus lors de la première réussite aujourd'hui
The parameter assigns to itself; the field is unchanged until this.name is used on the left.
Si alias et a réfèrent au même Student, alias.setName("Di") modifie le nom lu ultérieurement par a.
Difficulté 1/5 · 100 pièces + un petit bonus lors de la première réussite aujourd'hui
Les deux références identifient le même objet récepteur, dont le champ est mis à jour.
Le code dans l'objet b appelle a.setName("Cy"). À l'intérieur de setName, quel objet est this ?
Difficulté 1/5 · 100 pièces + un petit bonus lors de la première réussite aujourd'hui
Le récepteur a fournit this, indépendamment de l'objet dont le code a initié l'appel.
Fixing the shadowing bug with this:
- Instance variable
private String name; void setName(String name) { this.name = name; }— correct!this.name(the field) gets the value ofname(the parameter).
Carry the reasoning to a new case
- Trace a and b as separately created students named Ada and Bo.
a.setName("Cy")changes a to Cy while b stays Bo. If alias=a,alias.setName("Di")changes that same first object, so a now reads Di. - The receiver is the object the instance method acts on, not necessarily the object whose code called it. A static method can operate on an explicit object reference, but has no implicit this.
this refers to the current object — the one an instance method was called on. Its key use is resolving shadowing: this.name = name; assigns the parameter to the instance variable. Every instance method has an implicit this, but a static method has none, so this can't be used there.