• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Trying to figure out the "This" keyword

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 160
IntelliJ IDE VI Editor Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
     
    Ranch Hand
    Posts: 808
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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:
     
    Ranch Hand
    Posts: 73
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.)
     
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    To each. Isn't choice wonderful?
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I am neutral about redundant “this.” But welcome to the Ranch Chris Boon
     
    Everybody! Do the Funky Monkey! Like this tiny ad!
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic