ATTN AP Computer Science Lovers... An easy question about the "this" command!

Miami24
Tue, 30 Apr 2013 20:44:32 +0000

I apologize for my trivial and probably silly question, but I am a bit confused as to when to use the “this” prefix when using a method or accessing something.

For example, if we look at #4 here: Supporting Students from Day One to Exam Day – AP Central | College Board;

And we look at the solutions here: Supporting Students from Day One to Exam Day – AP Central | College Board;

We see that one solution to part a) is

public int countWhitePixels() {
int whitePixelCount = 0;
for (int row : this.pixelValues) {
for (int pv : row) {
if (pv == this.WHITE) {
whitePixelCount++;
}
}
}
return whitePixelCount;
}
while another solution is

public int countWhitePixels() {
int whitePixelCount = 0;
for (int row = 0; row < pixelValues.length; row++) {
for (int col = 0; col < pixelValues[0].length; col++) {
if (pixelValues[row][col] == WHITE) {
whitePixelCount++;
}
}
}
return whitePixelCount;
}

Here is my question. Why is it that they use the “this.” prefix when accessing pixelValues and even WHITE in the first solution, but not in the second? I thought “this” was implicit, so am I correct in saying “this.” is NOT necessary at all for the first solution?

Thank you SO much for your help :)