When to use “this” in Java

2019-06-20 02:36发布

问题:

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: http://apcentral.collegeboard.com/apc/public/repository/ap_frq_computerscience_12.pdf

And we look at the solutions here: http://apcentral.collegeboard.com/apc/public/repository/ap12_computer_science_a_q4.pdf

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 :)

回答1:

With this, you explicitly refer to the object instance where you are. You can only do it in instance methods or initializer blocks, but you cannot do this in static methods or class initializer blocks.

When you need this?

Only in cases when a same-named variable (local variable or method parameter) is hiding the declaration. For example:

private int bar;
public void setBar(int bar) {
    this.bar = bar;
}

Here the method parameter is hiding the instance property.

When coders used to use it?

To improve readability, it is a common practice that the programmers prepend the this. qualifier before accessing an instance property. E.g.:

public int getBar() {
    return this.bar;
    // return bar;  // <-- this is correct, too
}


回答2:

From The Java™ Tutorials

Using this with a Field

The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter.

For example, the Point class was written like this

public class Point {
    public int x = 0;
    public int y = 0;

    //constructor
    public Point(int a, int b) {
        x = a;
        y = b;
    }
}

but it could have been written like this:

public class Point {
    public int x = 0;
    public int y = 0;

    //constructor
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}


回答3:

When the name of a method parameter is the same as one of your class data member; then, to refer to the data member, you have to put this. before it. For example, in the function setA():

public void setA(int a)
{
    this.a = a;
}

Since both the data member and the papameter of the method is named a, to refer to the data member, you have to use this.a. In other cases, it's not required.

And, in your case, I don't think it's necessary to use the this, though there is no harm to use it.



回答4:

this refer to the instance of the class itself. Example:

private String name, car;

public class Car(String name, String color)
{
    this.name = name;
    this.color = color;
}


标签: java this