From the free course AP Computer Science A on Lykke
3.9 this Keyword
Key concepts: Java instance variables · private access modifier · static versus instance variables · constructors · constructor header syntax · block structure and indentation · return statements · grid and Location handling
Java uses the keyword this to identify the object whose instance method or constructor is currently running. That single reference becomes especially useful when a class contains fields and parameters with the same names.
3.9 this Keyword
Java uses the keyword this to identify the object whose instance method or constructor is currently running. That single reference becomes especially useful when a class contains fields and parameters with the same names.
One object, one set of instance fields
An instance variable is a field stored separately inside each object. A simulation might represent a feeder with its own food count and location, so two feeder objects can hold different values.
For Topic 3.9 this Keyword, the required declaration pattern uses private String and private int for instance variables. The keyword static must not appear: static creates one shared class variable rather than one variable per object.
public class Feeder
{
private String name;
private int foodAmount;
public Feeder(String name, int foodAmount)
{
this.name = name;
this.foodAmount = foodAmount;
}
}
Here, name on the left side means the field belonging to the current object, while name on the right side means the constructor parameter. Writing this.name removes the ambiguity. The same pattern initializes foodAmount.
static changes the meaning
Compare the storage models:
| Declaration | Storage meaning | Example consequence |
|---|---|---|
private int foodAmount; |
Each Feeder object has its own value |
One feeder can contain $20$ units while another contains $5$ |
private static int foodAmount; |
All Feeder objects share one value |
Changing one feeder’s amount changes the shared field |
Misconception check — “static just makes a field easier to access.” It does more than that: it changes the field from object-specific state to class-wide state. On a scored class-writing task, using static where an instance variable is required can both change the program’s behavior and lose the instance-variable point.
Constructor headers and initialization
A constructor is the special method that initializes a newly created object. Its header uses the class name and includes no return type—not even void.
public Feeder(String name, int foodAmount) // constructor
{
this.name = name;
this.foodAmount = foodAmount;
}
public void refill(int amount) // ordinary method
{
foodAmount += amount;
}
The constructor header is public Feeder(...), not public void Feeder(...). Adding void turns the apparent constructor into an ordinary method named Feeder; it will not serve as the required constructor. Constructor scoring may focus first on the exact header syntax, then on whether the parameters initialize the required fields.
Misconception check — “A constructor returns the object, so it needs a return type.” Java creates the object as part of new Feeder(...); the constructor itself has no declared return type and does not use return to return the object.
Block structure is determined by braces
Indentation makes Java readable, but curly braces determine the actual boundaries of constructors, methods, loops, and conditional blocks. Java does not require indentation for compilation, although poor indentation makes errors much harder to locate.
public int getFood()
{
if (foodAmount > 0)
{
return foodAmount;
}
return 0;
}
A calculated integer may be returned directly to earn the relevant method point. For example, return foodAmount; returns the current field value; it is not necessary to store that value in an extra variable first.
Applying the pattern: finding food in a grid
A feeder simulation involving birds or a bear might ask for a Location representing the next grid element. A correct method must inspect the element to the right and the element below while preventing an invalid location from being returned.
public Location getNextLoc(int row, int col)
{
if (row + 1 < grid.length && col + 1 < grid[0].length)
{
if (grid[row][col + 1] > grid[row + 1][col])
{
return new Location(row, col + 1);
}
return new Location(row + 1, col);
}
return null;
}
The guard condition checks both boundaries before creating a Location. Without it, a request involving the last row or last column could produce an out-of-bounds location. The exact strategy may vary, but the returned location must always be valid.
Skill connection
This topic exercises Computational Thinking Practice 1: Design Code when you decide what state belongs to each object, Computational Thinking Practice 2: Develop Code when you write the constructor and methods, and Computational Thinking Practice 3: Analyze Code when you trace this, shared state, braces, and boundary guards. Computational Thinking Practice 4: Document Code and Computing Systems supports clear comments and indentation, while Computational Thinking Practice 5: Use Computers Responsibly remains relevant when simulations model living organisms or collect data.
Retrieval check: correct the class
Identify four issues in this code: which declaration is an instance field, which name is a constructor parameter, what is wrong with the constructor, and which value is incorrectly shared?
public class Bird
{
private static String species;
private int count;
public void Bird(String species, int count)
{
this.species = species;
this.count = count;
}
}
The intended correction is private String species;, because each Bird object should store its own species value; species in the constructor is the parameter; and public void Bird(...) must become public Bird(...). The count field is already instance-specific because it is not static.

Source Materials
- AP Computer Science A Course and Exam Description
- AP Computer Science A Course and Exam Description Clarifications and Corrections
- AP Computer Science A Course Overview
- AP Computer Science A Course at a Glance
- AP Computer Science A Course at a Glance Poster
- Adopt AP Computer Science A
- AP Computer Science A
- AP Computer Science A