Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a good reason to use parameters that shadow fields?

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;
    }
}
like image 731
user1888645 Avatar asked Dec 09 '12 00:12

user1888645


1 Answers

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

like image 170
amit Avatar answered Oct 09 '22 15:10

amit