When to use "this" in Java

I apologize for my trivial and probably stupid question, but I'm a little confused when to use the "this" prefix when using a method or accessing something.

For example, if we look at No. 4 Here: http://apcentral.collegeboard.com/apc/public/repository/ap_frq_computerscience_12.pdf

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

We see that one solution for 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; } 

whereas another solution

  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 do they use "this." prefix when accessing pixelValues ​​and even WHITE in the first solution, but not in the second? I thought this was implicit, so I correctly say this. NOT required at all for the first solution?

Thank you very much for your help :)

+6
source share
4 answers

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

When do you need it?

Only in cases where the variable of the same name (local variable or method parameter) hides the declaration. For instance:

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

Here, the method parameter hides the instance property.

When did coders use it?

To improve readability, a common practice is for programmers to add this. qualifier this. before accessing the instance property. For instance:.

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

From Java β„’ Tutorials

Using it with a field

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

For example, the Point class was written as follows:

 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 be 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; } } 
+4
source

If the name of the method parameter matches the name of your class data item; then, to access the data element, you need to put this. in front of it this. . For example, in the setA() function:

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

Since both the data element and the method parameter are named a , to refer to the data element, you should use this.a In other cases, this is not required.

And, in your case, I don’t think you need to use this , although there is no harm in using it.

+1
source

this refers to an instance of the class itself. Example:

 private String name, car; public class Car(String name, String color) { this.name = name; this.color = color; } 
0
source

Source: https://habr.com/ru/post/944011/


All Articles