This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

This Keyword

 
podonga poron
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
jaspreet atwal
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
podonga poron
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some old threads discussing the same issue: here, and here. You can find more with the SEARCH button above.
 
podonga poron
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic