AP® Computer Science A glossary
this
Also called: this keyword, this reference
Course: AP® Computer Science A Shows up in: Unit 3: Class Creation
A special variable inside an instance method or constructor that holds a reference to the current object, the object whose method or constructor is running.
Appears in the course and exam description (CED): 3.9.A.13.9.A.23.9.A.3
Use this.name to reach an instance variable hidden by a parameter, and pass this to give another method a reference to the current object. Class (static) methods have no this.
public class Runner
{
private String name;
public Runner(String name)
{
this.name = name; // instance variable = parameter
}
public static void main(String[] args)
{
Runner r = new Runner("Ivy");
System.out.println(r.name);
}
}
Output:
Ivy Learn it properly, with a checkpoint 3.9 this Keyword