|
Page 1 of 1
|
[ 2 posts ] |
|
mkalaiyarasan
As a Family
Joined:
Tue Aug 25, 2009 5:25 pm
Beans: 35
In Bank: 0
College: studied
Degree: BE
Department: cse
State: Tamilnadu
Fav Quote: cse
Position: Employee
|
|
Thu Sep 03, 2009 7:10 am
|
Post subject: Shadowing field names and this
By making use of the this keyword, you can even use the same name for arguments to the constructor (or any other method) as you use for field names. For example, class TwoDPoint {
double x; double y; TwoDPoint(double x, double y) { this.x = x; this.y = y; } String getAsString() { return "(" + this.x + "," + this.y + ")"; } void setX(double x) { this.x = x; } void setY(double y) { this.y = y; } double getX() { return this.x; } double getY() { return this.y; } } Inside a method, a declaration of a variable or argument with the same name as a field shadows the field. You can refer to the field by prefixing its name with this.
|
|
|
|
Kumari
lecturer
Joined:
Fri Sep 04, 2009 6:01 pm
Beans: 33
Location: India
In Bank: 50
College: Vaishnav
Degree: M.E
Department: Mech
State: Kerala
Position: Lecturer
|
|
|
|
|
Page 1 of 1
|
[ 2 posts ] |
|