i was reading the sun tutorials, then i came trough the part of the this keyword.
can anyone explain me why to use the this keyword, as long as i see, it's only more typing.
I DID READ THE SUN TUTORIALS, BUT I DIDN'T UNDERSTAND IT FROM THERE!!!
Printable View
i was reading the sun tutorials, then i came trough the part of the this keyword.
can anyone explain me why to use the this keyword, as long as i see, it's only more typing.
I DID READ THE SUN TUTORIALS, BUT I DIDN'T UNDERSTAND IT FROM THERE!!!
That silly message in red is making my eyes hurt when I try to read your message.
Here's a very basic simple example
Notice how you have to use the this keyword to specify you are talking about the variables in the class not the ones in the method.Code:public class Size {
int width;
int height;
public Size(int width, int height) {
this.width = width;
this.height = height;
}
}
that's just what i don't understand, if you would just have typed width without this before it, the output would be same i think/
how can it be the same
in the method parameters you height and width and also in the class you width and height
if you do this
then its like just overwriting your method's width and method's height, which are temporary.Code:public Size(int width, int height) {
width = width;
height = height;
}
but when you do this
it stores the values of method's width and height in a certain int values.(over here width and height in the class.private variables)Code:public Size(int width, int height) {
this.width = width;
this.height = height;
}
What output?
As soon as you get inside that size constructor, you now have two width variables in scope.
The instance variable width (declared outside the constructor) and the local variable width (the one passed into the constructor). The one in the constructor hides the one declared outside because it is more local. If you thus want to access the one outside the constructor you would need to use this to get to it.
No, that is what that example is all about: there are two 'width' identifiers: one is a parameter and the other one belongs to an object for which the method was called; the parameter shadows the name of the member variable so 'this.width' is needed there. If you leave out the 'this.' part you assign the parameter to itself (that doesn't do anything).
kind regards,
Jos
i have seen an other post wiht same question so here is an other approach of this keyword i'll use the class StormyWaters used:
you have 2 variables width and height declared in class that means they will be seen in all the methods you will writeCode:public class Size {
int width;
int height;
public Size(int width, int height) {
this.width = width;
this.height = height;
}
}
on the constrouctor method you need 2 new variables that will store the values that will be used when you will create an object, variables that have same names with the global variables from class so if you don't use "this" the compiler will not know which variable you refer to. you can rename the local variables of the method and there will be no need to use "this":
it's kind of silly but works :DCode:public class Size {
int width;
int height;
public Size(int wth, int hght) {
wth = width;
hght = height;
}
}
It doesn't work the way it's supposed to.
See this code that uses your constructor. You should see that your constructor is wrong. The 'this' keyword is your friend. Learn it.
Code:class Size {
int width;
int height;
public Size(int wth, int hght) {
wth = width;
hght = height;
}
@Override
public String toString() {
return "My size is width=" + width + " and height=" + height;
}
public static void main(String[] args) {
Size size42 = new Size(42, 42);
System.out.println(size42);
}
}
yes you are right that code is not working the way it's supposed to but that does mean that the ideea of not useing the "this" keywold is wrong it's quite some time since i haven;t use it so i I made a little mistake here is the working code
well not the local variables of the constructor take the values of the class variables .... it's the other way the class variables take the values of the constructor this cod is actually working and i didn't said it's a good ideea to not use the keyword this :)Code:class Size {
int width;
int height;
public Size(int wth, int hght) {
width = wth;
height = hght;
}
@Override
public String toString() {
return "My size is width=" + width + " and height=" + height;
}
public static void main(String[] args) {
Size size42 = new Size(42, 42);
System.out.println(size42);
}
}
Now you have variables with strange names in the code. A variable should try as much as possible to say what it represents.