Results 1 to 11 of 11
Thread: this keyword
- 01-12-2010, 02:24 PM #1
Senior Member
- Join Date
- Dec 2009
- Posts
- 104
- Rep Power
- 0
this keyword
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!!!Beginner in Java Programming, Please don't trust my anwsers blind please :D
- 01-12-2010, 02:28 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
That silly message in red is making my eyes hurt when I try to read your message.
- 01-12-2010, 02:33 PM #3
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
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.Java Code:public class Size { int width; int height; public Size(int width, int height) { this.width = width; this.height = height; } }
- 01-12-2010, 05:20 PM #4
Senior Member
- Join Date
- Dec 2009
- Posts
- 104
- Rep Power
- 0
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/
Beginner in Java Programming, Please don't trust my anwsers blind please :D
- 01-12-2010, 05:33 PM #5
Senior Member
- Join Date
- Oct 2009
- Location
- California,US
- Posts
- 201
- Rep Power
- 4
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.Java 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)Java Code:public Size(int width, int height) { this.width = width; this.height = height; }
- 01-12-2010, 05:33 PM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 01-12-2010, 05:34 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,411
- Blog Entries
- 7
- Rep Power
- 17
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
- 02-28-2010, 09:24 PM #8
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 writeJava Code: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 :DJava Code:public class Size { int width; int height; public Size(int wth, int hght) { wth = width; hght = height; } }silence i'm trying to meditate:p
- 03-01-2010, 07:21 AM #9
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
Java 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); } }
- 03-01-2010, 09:02 AM #10
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 :)Java 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); } }silence i'm trying to meditate:p
- 03-01-2010, 09:20 AM #11
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Similar Threads
-
Keyword : volatile
By peiceonly in forum Advanced JavaReplies: 14Last Post: 01-18-2011, 06:15 PM -
use of static keyword
By venkatallu in forum New To JavaReplies: 2Last Post: 06-25-2009, 07:53 AM -
what is the use of final keyword
By sumanandjesus in forum Advanced JavaReplies: 9Last Post: 03-27-2009, 12:50 PM -
transient keyword
By Java Tip in forum Java TipReplies: 0Last Post: 12-11-2007, 10:27 AM -
Use of this keyword
By Java Tip in forum Java TipReplies: 0Last Post: 11-18-2007, 07:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks