Please anyone can help me to understand how works the "this.variable" command on java.
Anyone can help to explain how works the "this.variable"help to undertsand that
Page 1 of 1
7 Replies - 921 Views - Last Post: 24 April 2009 - 12:32 PM
#1
Anyone can help to explain how works the "this.variable"
Posted 23 April 2009 - 03:14 PM
Replies To: Anyone can help to explain how works the "this.variable"
#2
Re: Anyone can help to explain how works the "this.variable"
Posted 23 April 2009 - 04:07 PM
what is the use of "this." on java and when we have to use it ?
#3
Re: Anyone can help to explain how works the "this.variable"
Posted 23 April 2009 - 04:08 PM
Sure, say you have a class like the following:
We have the instance variable "a" but in an act of terrible design, there is also a variable "a" local to the method. This hides the instance variable so the first line printed is "7". How do we get to the value stored in the instance variable? We use this.a
Hope that helps!
class Demo {
private int a = 5;
void showOutput(int a) {
System.out.println(a);
System.out.println(this.a);
}
public static void main(String[] args) {
Demo demoInstance = new Demo();
demoInstance.showOutput(7);
}
}
We have the instance variable "a" but in an act of terrible design, there is also a variable "a" local to the method. This hides the instance variable so the first line printed is "7". How do we get to the value stored in the instance variable? We use this.a
Hope that helps!
#4
Re: Anyone can help to explain how works the "this.variable"
Posted 23 April 2009 - 04:11 PM
You use "this" when you want to explicitly refer to an Object.
#5
Re: Anyone can help to explain how works the "this.variable"
Posted 23 April 2009 - 04:21 PM
W3B5T4R, on 23 Apr, 2009 - 03:11 PM, said:
You use "this" when you want to explicitly refer to an Object.
Not neccisarily. this. means your referring to the variable of the class. It is as foley said, you may have a class variable the same name as the param name, so you would use this.classVariable and then param variable.
this. simply means it belongs to the Class. Can be an object, variable , ect.
#6
Re: Anyone can help to explain how works the "this.variable"
Posted 23 April 2009 - 04:53 PM
It can also be useful when an instance needs to pass itself as a method argument.
#7
Re: Anyone can help to explain how works the "this.variable"
Posted 23 April 2009 - 05:40 PM
W3B5T4R, on 23 Apr, 2009 - 03:11 PM, said:
You use "this" when you want to explicitly refer to an Object.
Yes... this references the object which is an instance of the class into which you are
class Abc {
int x;
void method(Abc obj) {
obj.x = 10; // set to 10 the instance variable x of the Abc object passed as parameter
x = 5; // set to 5 the instance variable x of the object I am
this.x = 5; // same thing but you need the "this" when ambiguous
}
void steX(Abc obj, int x) {
obj.x = x; // set the instance variable x of the Abc object passed as parameter to the value of x passed also as parameter
this.x = x; // here the "this" is necessary so say that we want to set to the value of x passed as parameter the instance variable x of this object
}
}
This post has been edited by pbl: 23 April 2009 - 05:46 PM
#8
Re: Anyone can help to explain how works the "this.variable"
Posted 24 April 2009 - 12:32 PM
As its mentioned in above comments, we use this to refer to the class variables. I normally use this keyword when I reuse the variable names.
Class foo_Area
{
int height;
int width;
int area;
public foo_Area(int height, int width)
{
this.height = height;
this.width = width;
this.area = height*width;
}
/*
more codes get set here
*/
}
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|