View RSS Feed

Java Object

Using this with a Field

Rate this Entry
by , 04-26-2012 at 04:40 PM (317 Views)
The this keyword usage has a reason that field gets shadowed by constructor parameter or method.

For example the Point class is as following written:
Java Code:
public class Point {
    public int x = 0;
    public int y = 0;
        
    //constructor
    public Point(int a, int b) {
        x = a;
        y = b;
    }
}
However, other way to write it is;
Java Code:
public class Point {
    public int x = 0;
    public int y = 0;
        
    //constructor
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

Submit "Using this with a Field" to Facebook Submit "Using this with a Field" to Digg Submit "Using this with a Field" to del.icio.us Submit "Using this with a Field" to StumbleUpon Submit "Using this with a Field" to Google

Comments