Is there a good reason to use parameters that shadow fields? What is the difference between these two:
public class Point {
public int x = 0;
public int y = 0;
//constructor
public Point(int a, int b) {
x = a;
y = b;
}
}
and
public class Point {
public int x = 0;
public int y = 0;
//constructor
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
And what if you use the this
keyword without parameters that shadow fields in this example (I'm guessing it's just unnecessary):
public class Point {
public int x = 0;
public int y = 0;
//constructor
public Point(int a, int b) {
this.x = a;
this.y = b;
}
}
I think it is a style issue, but especially for public fields - Point(int x,int y)
is self documenting itself, while Point(int a, int b)
doesn't
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With