*
The moose likes Beginning Java and the fly likes This Keyword Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "This Keyword" Watch "This Keyword" New topic
Author

This Keyword

podonga poron
Ranch Hand

Joined: May 12, 2008
Posts: 55
JButton button = new JButton();
button.addActionListener(this);

what is the meaning of the "this" keyword here ??

/* lol i was searching for this in the net before posting and i found a band !! lol !!! http://www.myspace.com/addactionlistenerthis */
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Within an instance method or constructor, this is a reference to the current object.

So in the example you give, the assumption is that the code is part of an instance method or constructor of an object that implements java.awt.event.ActionListener, hence it's adding itself to the button.


There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
jaspreet atwal
Ranch Hand

Joined: Sep 05, 2007
Posts: 52
for example:


the above is similar to


so this() is a call to a class's own constructor and returns an object of its own type.


Still Learing..
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Originally posted by jaspreet atwal:
for example:


the above is similar to


so this() is a call to a class's own constructor and returns an object of its own type.


Umm, sorry, that's wrong on a number of accounts.

First, the object added as an ActionListener has to implement the ActionListener interface.

Secondly, this() would appear to be be a call to a method inadvisedly named this -- and wouldn't compile. The keyword does not take parameters, so no parentheses.

Thirdly, a call to this() does not invoke the "class's own constructor", and a constructor does not "return" an object, it constructs it. The new keyword in conjunction with a call to a constructor returns a reference to a newly constructed object.


luck, db
There are no new questions, but there may be new answers.
podonga poron
Ranch Hand

Joined: May 12, 2008
Posts: 55
hey ! thanks for your replies !!

this is the complete code ..

what is the "this" keyword doing in here ? is sending data to a constructor ?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37902
    
  22
As well as Darryl Burke's corrections, I would point out that in most cases

addActionListener(this)

fails to constitute object-oriented programming; I fulminate about it at regular intervals on the Swing forum.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37902
    
  22
Some old threads discussing the same issue: here, and here. You can find more with the SEARCH button above.
podonga poron
Ranch Hand

Joined: May 12, 2008
Posts: 55
Originally posted by Campbell Ritchie:
Some old threads discussing the same issue: here, and here. You can find more with the SEARCH button above.


yes man, i used the search before posting, and i know what is the use of this and super keywords .. but i have a confunsion in this specific case ..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37902
    
  22
Do you mean what does this mean in addActionListener(this)?

It means that the object you are working inside (which can be regarded as an ActionListener) passes itself as a parameter to the addActionListener() method of some other object which is a variable (or field) of the present object.

Which is the same as Stevi Deter told you earlier.

And look at the actionPerformed() method. The number of awkward if-else blocks in it shows what a bad bit of design it is.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: This Keyword
 
Similar Threads
using Arrays to store info
The Facebook thing
The "This" keyword?
What makes the iPOD sound so good?
How to execute this URL mentioned below