aspose file tools*
The moose likes Beginning Java and the fly likes Trying to figure out the Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trying to figure out the "This" keyword" Watch "Trying to figure out the "This" keyword" New topic
Author

Trying to figure out the "This" keyword

John Quach
Ranch Hand

Joined: Sep 28, 2011
Posts: 50
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

Joined: Jul 26, 2007
Posts: 10523
    
    9

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)


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
John Quach
Ranch Hand

Joined: Sep 28, 2011
Posts: 50
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

Joined: Jul 26, 2007
Posts: 10523
    
    9

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

Joined: Apr 23, 2009
Posts: 160

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.


"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - George Bernard Shaw
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
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

    Joined: Mar 12, 2011
    Posts: 808
    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

    Joined: Mar 11, 2011
    Posts: 63
    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

    Joined: Jan 10, 2002
    Posts: 61653
        
      67

    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.)


    [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    Chris Boon
    Greenhorn

    Joined: Oct 17, 2011
    Posts: 2
    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

    Joined: Jan 10, 2002
    Posts: 61653
        
      67

    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

    Joined: Mar 12, 2011
    Posts: 808
    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

    Joined: Jan 10, 2002
    Posts: 61653
        
      67

    To each. Isn't choice wonderful?
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39834
        
      28
    I am neutral about redundant “this.” But welcome to the Ranch Chris Boon
     
     
    subject: Trying to figure out the "This" keyword