Results 1 to 11 of 11

Thread: this keyword

  1. #1
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default 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

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    That silly message in red is making my eyes hurt when I try to read your message.

  3. #3
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Here's a very basic simple example

    Java Code:
    public class Size {
         int width;
         int height;
    
         public Size(int width, int height) {
              this.width = width;
              this.height = height;
         }
    }
    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.

  4. #4
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    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

  5. #5
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    Quote Originally Posted by coltragon View Post
    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
    Java Code:
         public Size(int width, int height) {
              width = width;
              height = height;
         }
    then its like just overwriting your method's width and method's height, which are temporary.

    but when you do this

    Java Code:
         public Size(int width, int height) {
              this.width = width;
              this.height = height;
         }
    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)

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,522
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by coltragon View Post
    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/
    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

  8. #8
    Dumisan's Avatar
    Dumisan is offline Member
    Join Date
    Feb 2010
    Location
    Romania
    Posts
    30
    Rep Power
    0

    Default

    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:
    Java Code:
    public class Size {
         int width;
         int height;
    
         public Size(int width, int height) {
              this.width = width;
              this.height = height;
         }
    }
    you have 2 variables width and height declared in class that means they will be seen in all the methods you will write

    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":
    Java Code:
    public class Size {
         int width;
         int height;
    
         public Size(int wth, int hght) {
              wth = width;
              hght = height;
         }
    }
    it's kind of silly but works :D
    silence i'm trying to meditate:p

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by Dumisan View Post
    it's kind of silly but works :D
    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);
    	}
    }

  10. #10
    Dumisan's Avatar
    Dumisan is offline Member
    Join Date
    Feb 2010
    Location
    Romania
    Posts
    30
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    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.
    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
    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);
    	}
    }
    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 :)
    silence i'm trying to meditate:p

  11. #11
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Now you have variables with strange names in the code. A variable should try as much as possible to say what it represents.

Similar Threads

  1. Keyword : volatile
    By peiceonly in forum Advanced Java
    Replies: 13
    Last Post: 11-17-2010, 12:47 PM
  2. use of static keyword
    By venkatallu in forum New To Java
    Replies: 2
    Last Post: 06-25-2009, 07:53 AM
  3. what is the use of final keyword
    By sumanandjesus in forum Advanced Java
    Replies: 9
    Last Post: 03-27-2009, 12:50 PM
  4. transient keyword
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-11-2007, 10:27 AM
  5. Use of this keyword
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-18-2007, 07:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •