This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trying to figure out the "This" keyword

 
John Quach
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I might be missing something fundamental. I'm trying to understand why "This" is needed.

I'm comparing these two similar classes. One of them uses (This) while the other doesn't. I have some insight on what's going on but I can't quite put my finger on it.

Here are the two classes. Can anyone explain what "This" is about? No pun intended. I've looked online myself and some books, but I can't understand it.





Here's the second class.

 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider two houses. John's house (A) and Maneesh's house (B). When you are inside A, and you say I am in this house, you are referring to A (or John's house). Similarly when you say this in your code, you are referring to the current instance.

The code addActionListener needs an ActionListener argument.
1) You can have a class implement ActionListener (LoginImplementAL). Since you are adding the action listener from inside LoginImplementAL you can say this
2) You can have a separate class implementing ActionListener (LoginExtendsJFrame) and then pass a reference for that instance as the argument)
 
John Quach
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After reading your analogy, the guides I'm reading are starting to make some sense. I understand a constructor can use the keyword "this" to have its argument type in the parameters have the same identifier as its local variables ( sorry if I got any of that wrong). The only reason I would see anyone do that is to not cause any confusion with the names in the whole program.

It seems to me, however, that there's no other way around the keyword "this" if the class itself is implementing ActionListener like in the LoginImplementsAL class.

Is there another argument other than "this" that this method call will accept in the LoginImplementAL?



 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Quach wrote:
Is there another argument other than "this" that this method call will accept in the LoginImplementAL?

Anything which implements ActionListener is absolutely acceptable.
Since the LoginImplementAL class implements it, as you already know, you can pass "this"
OR
Any other instance which implements ActionListener (like your TextFieldHandler)
OR
An anonymous class like this
 
Riaan Nel
Ranch Hand
Posts: 160
IntelliJ IDE Ubuntu VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh gave you a great explanation on the this keyword already, but I would like to add to that.

Generally, this serves little purpose apart from assisting with the readability of your code when it's used in the below context. Method calls and variable assignments will always work with the object instance in which they are called, regardless of whether or not you specify this.


The only exception (AFAIK) is when you have a local variable with the same name, which "hides" the class variable. You'll often see this in setter methods.


Another use of this is to reference constructors from other constructors in the same class. E.g., if you had a TennisBall class and tennis balls can be any colour, but they are yellow by default, you could implement it like this. The major advantage of this approach is the fact that you can keep all of your actual logic in a single constructor, instead of having to duplicate code all over. See below.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Maneesh. I am always wary of books which use addActionListener(this); because that often leads to poor, non object-oriented design.

I would tell you to search, but searching for “this” will produce thousands and thousands of hits

Three uses of the keyword this
  • 1: Refer to the current object as a whole; you have already seen that in this thread.
  • 2: Followed by a dot: allows you to refer to a member of the current object as opposed to a local variable or parameter. Commonly used in constructors and “setXXX” methods.
  • 3: Followed by round brackets (): calls constructors of the current object: only ever used as the first line of a constructor, and rather similar to super().
  • In all cases it means the current object. You must not use this in a static context.
     
    dennis deems
    Ranch Hand
    Posts: 808
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Great responses! I want to add that my favorite use of this is the implementation of toString() in the String class:
     
    Ron Ingram
    Ranch Hand
    Posts: 73
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I also think a good use is a case where you have an instance variable and a local variable.. you would use "this" for the local variable to make a determination between the two.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 64696
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ron Ingram wrote:I also think a good use is a case where you have an instance variable and a local variable.. you would use "this" for the local variable to make a determination between the two.

    That is completely incorrect. this would be used for the instance variable, not the local variable (and this has already been covered in this topic.)
     
    Chris Boon
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Riaan Nel wrote:Maneesh gave you a great explanation on the this keyword already, but I would like to add to that.

    Generally, this serves little purpose apart from assisting with the readability of your code when it's used in the below context. Method calls and variable assignments will always work with the object instance in which they are called, regardless of whether or not you specify this.



    This is the first post I looked at and I think its solved my problem! :-)

    I'm using Eclipse (someone recommended it, seems as good as any) - however I've noticed that it will only give me the drop downs to remember superclass methods when I use "this" - see here:


    (As you can see, I'm just getting started with a "From scratch" book!)

    Where the four "this" statements are - if I omit them, it still compiles and runs fine. BUT if I type "this" the IDE gives me the methods on a drop down - quite handy when I can't remember exactly what they're called.

    Question is, should I not be doing this? Is this a bad habit that I'll regret down the line? Is there a reason to do the above either with or without "this"?

    Thanks! :-)
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 64696
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I always prefix references to instance variables with this. whether its required or not. This makes references to instance variables consistent throughout the class code. I don't like things meaning something different depending upon context.

    Some people hate this style, but it's entirely up to you.
     
    dennis deems
    Ranch Hand
    Posts: 808
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Bear Bibeault wrote:I always prefix references to instance variables with this. whether its required or not. This makes references to instance variables consistent throughout the class code. I don't like things meaning something different depending upon context.

    Some people hate this style, but it's entirely up to you.


    Count me among the haters. I think unnecessary use of "this" clutters up the code.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 64696
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    To each. Isn't choice wonderful?
     
    Campbell Ritchie
    Sheriff
    Posts: 48652
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I am neutral about redundant “this.” But welcome to the Ranch Chris Boon
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic