Hello, as I am steadily adjusting to java and starting to compose classes. I am having trouble with the this. statement. Ive searched and read up on the java site, as well as read the section on this. statement in absolute java and apparently am not making the connection on what it actually does. If anyone would be so kind to either explain to me its function or point me to a web source I can read up on it.
Any advice is greatly appreciated.
Thanks,
this. statement
Page 1 of 15 Replies - 389 Views - Last Post: 14 October 2009 - 09:30 PM
Replies To: this. statement
#2
Re: this. statement
Posted 14 October 2009 - 08:56 PM
Basically, you use the keyword this for one of two main reasosns.
One- You have a variable that is shadowed by a parameter or local variable. The keyword this refers to the global variable and the other field is the local variable as shown below:
The other reason to use the keyword this is to call another constructor, like so:
As you can see, the constructor w/o parameters makes a reference to the other constructor using the keyword this in order to set the default values to 0.
One- You have a variable that is shadowed by a parameter or local variable. The keyword this refers to the global variable and the other field is the local variable as shown below:
class A{
int a;
public A(int a){
this.a = a;
}
}
The other reason to use the keyword this is to call another constructor, like so:
class Rectangle{
public int width, height, x, y;
public Rectangle(){
this(0,0,0,0);
}
public Rectangle(int width, int height, int x, int y){
this.width = width;
this.height = height;
this.x = x;
this.y = y;
}
}
As you can see, the constructor w/o parameters makes a reference to the other constructor using the keyword this in order to set the default values to 0.
#3
Re: this. statement
Posted 14 October 2009 - 09:02 PM
this makes the difference between instance variables and parameter variables:
You can do
Here there is no ambiguity... there is only one x and one a
so the a value received as parameter is assigned t the instance variable x
but Java programmers like to have the parameters variable name to match the instance variable name so you have to say
this.x <--- the class x
= x <--- the parameter
class Rectangle {
int x, y, w, h; <---- these are instance variables proper to the class
// constructor
Rectangle(int x, int y, int w, int h) {
this.x = x;
the statement this.x = x; means
have the instance variable x of the class Variable the one pointed bt <----
to have the value x received as parameter in the constructor statement Rectangle(x, y, w, h) {
You can do
class Rectangle {
int x, y, w, h; <---- these are instance variables proper to the class
// constructor
Rectangle(int a, int b, int c, int d) {
x = a;
Here there is no ambiguity... there is only one x and one a
so the a value received as parameter is assigned t the instance variable x
but Java programmers like to have the parameters variable name to match the instance variable name so you have to say
this.x <--- the class x
= x <--- the parameter
#4
Re: this. statement
Posted 14 October 2009 - 09:03 PM
It basically means that an object has a reference to itself, which can be useful for clarifying scope of a field.
In the above, this. is used to indicate that this.x is referring to the class member x, not the setX(x) argument. Java searches at method level first and if it can not fine something there, it looks at class members.
Hope that helps.
scrat
public class Bob {
private String x;
public String getX() {
return x;
}
public void setX(String x) {
this.x = x;
}
}
In the above, this. is used to indicate that this.x is referring to the class member x, not the setX(x) argument. Java searches at method level first and if it can not fine something there, it looks at class members.
Hope that helps.
scrat
#5
Re: this. statement
Posted 14 October 2009 - 09:04 PM
Sorry Macos .... 2 cross posts tonigh
#6
Re: this. statement
Posted 14 October 2009 - 09:30 PM
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote








|