this 키워드
도입됨
7:20
클래스 생성
여기에는 이름, 학년, 등급이라는 세 개의 독립된 변수로 저장된 한 학생이 있습니다. 이것은 작동합니다. 이제 학생 천 명을 추가하십시오. 삼천 개의 변수가 필요합니다…
영어 내레이션 · 영어와 중국어 자막 전환 가능
이 수업의 내용
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.

"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는 수신자 객체를 가리킴
setName 내부에서 this는 이 메서드를 호출한 ada 객체입니다.
인스턴스 메서드 내부에서 this는 ...를 가리킵니다.
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
this는 현재 객체에 대한 핸들입니다.
setName(String name) { this.name = name; }에서 this.name은 ...입니다
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
this.name은 필드이며, plain name은 파라미터입니다.
this 키워드는 정적 메서드 내부에서도 사용할 수 있습니다.
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
정적 메서드는 특정 객체가 없으므로 this도 없습니다.
setName(String name) 내부에서 name = name; (this 없음)는 무엇을 하겠습니까?
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
두 이름 모두 파라미터입니다; 필드에 접근하려면 this.name을 사용해야 합니다.
显式로 작성하든 안 하든 모든 인스턴스 메서드에는 암묵적인 this가 있습니다.
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
인스턴스 메서드에는 현재 수신자가 존재합니다. 무규격 score는 매개변수나 로컬 변수가 해당 필드를 가림(shadows)하지 않을 때만 this.score을 의미합니다.
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.
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
The parameter assigns to itself; the field is unchanged until this.name is used on the left.
alias과 a가 동일한 Student를 가리킬 경우, alias.setName("Di")로 변경된 이름은 이후 a를 통해 읽히게 됩니다.
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
두 참조 모두 동일한 수신자 객체를 지칭하며, 이 객체의 필드가 업데이트됩니다.
객체 b의 코드가 a.setName("Cy")를 호출합니다. setName 내부에서 this는 어떤 객체를 가리키나요?
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
수신자 a가 this를 제공하며, 이는 다른 어떤 객체의 코드에 의해 호출이 시작되었든 상관없습니다.
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.
어떤 문제가 있었나요? 사이트 팀만 이 내용을 볼 수 있습니다.