Results 1 to 9 of 9

Thread: this

  1. #1
    blug is offline Member
    Join Date
    Oct 2010
    Posts
    26
    Rep Power
    0

    Default this

    Hey guys, Im a bit confused. Im studying for my exams but I hit a bit of a snag. Looking in examples I see alot..

    variable1 = this;

    I have used this before in methods like...

    public int randomMethod(int lolol){
    this.lolol = lolol;
    return lolol;
    }

    What does "this" on its own refer to?

    Thanks!

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    "this" is a reference to the instance whose method has been called. It has the same meaning in the expression "this.lolol=lolol".

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Why on earth someone would assign a variable to refer to 'this' is beyond me. Pass to a method, return from a method, sure, but assign? no clue - seems a bit redundant if you ask me.

    Anyway, as pbrockway said, it refers to the instance who's method has been called, or the current instance of the class you are in.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  4. #4
    blug is offline Member
    Join Date
    Oct 2010
    Posts
    26
    Rep Power
    0

    Default

    Thanks guys, and yeah singing, the book I am reading has a lot of toy examples =D

  5. #5
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Ah, its from a book. That explains it then... The redundancy in programming books and official tutorials never ceases to amaze me :rolleyes:
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default

    Quote Originally Posted by Singing Boyo View Post
    Ah, its from a book. That explains it then... The redundancy in programming books and official tutorials never ceases to amaze me :rolleyes:
    This of it this way: if that example hadn't been in the book, blug still wouldn't understand the concept of the this keyword.

    Now do you still think it's redundant?

    db

  7. #7
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Singing Boyo View Post
    Why on earth someone would assign a variable to refer to 'this' is beyond me.

    look at this link

    The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter.
    my personal opinion: even if the keyword is not necessery in a piece of code it makes the code more comprehensible.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by j2me64 View Post
    look at this link



    my personal opinion: even if the keyword is not necessery in a piece of code it makes the code more comprehensible.

    I think it was assignment of this to some other variable to some other variable that SingingBoyo was drawing attention to. Which I've never had occasion to use (for whatever that's worth).

  9. #9
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default Redundancy is not Uselessness...

    Quote Originally Posted by Darryl.Burke View Post
    This of it this way: if that example hadn't been in the book, blug still wouldn't understand the concept of the this keyword.

    Now do you still think it's redundant?

    db
    Its still redundant. Not necessarily useless, but it's certainly redundant to show examples that will never/extremely rarely be used, just to show the full capability of a keyword. Assigning something to refer to 'this' is inherently redundant. What are you going to do with it? v = this; v.method(); is a bit pointless if you ask me.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Posting Permissions

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