1

I'm new to Java/Android. I don't understand the "this" in addActionListener(this). I have read many books and forum, but I still confuse the following:

Someone explain: "this" is a refernce to the current object"

Register an instance of the event handler class as a listenser on one or more components. someComponent.addActionListener(instanceofMyClass);

OK, I understand, it is an object of a class.

However, someone explain: "this" represents an implemented and instantiated ActionListener, which happens to be your class.

So "this" can be an object of a class and also a "class". This is what I don't understand.

Would someone explain clearly to me. Thanks!

user1232250
  • 329
  • 3
  • 19

4 Answers4

2

This always refers to the current object, its not a class. Sometimes people wrongfully say class when they meant object, thats all.

0x6C38
  • 6,796
  • 4
  • 35
  • 47
  • Hi I have a doubt regarding this please refer to this [example](http://stackoverflow.com/a/18585247/3228960) note the line in ON CREATE MyButton m = new MyButton(this); as you said this refers to object, but in the MyButton class constructor it is typecasted to Interface how does this happen? My understanding is as he passed this( an object) so in constructor he should mention an object(MyActivity object) not an interface. Sorry if you mind this as stupid question. – srujan maddula Jan 23 '15 at 14:03
  • 1
    @srujan maddula in that context `ml` is an object of any class that implements `MyListener` interface. Its called [polymorphism](http://stackoverflow.com/a/3110318/2180290). – 0x6C38 Jan 23 '15 at 17:20
2

"this" refer's to the current object, since Java uses methods to change Objects. So, when you call "this" in your Activity, you are giving your method something to change.

Your code "someComponent.addActionListener(instanceofMyClass);" is doing the exact same thing. You are taking the object "someComponent" and using the method "addActionListener". The ActionListener is then going to wonder where it is going to get the listener code, and you are stating that you want it to be called from "instanceofMyClass", which could be swapped with "this"

You could find another explanation with code here: What is the meaning of "this" in Java?

Community
  • 1
  • 1
clever_trevor
  • 1,530
  • 2
  • 22
  • 42
  • So,if this always refer to an current object, what is the current object? There is no object created. If I create a button, is the button is the current object when someone press the button? – user1232250 Oct 13 '13 at 02:52
  • If you call "this", it will reference the object created by the constructor, which the constructor is SomeClass(). Once you have the reference to the SomeClass() object, you can use its methods/variables. When someone clicks on a button and you call "this.method()", it would do the same thing as "method" in your class. I'd recommend just adding some variables and messing around with "this" and just the variables alone. Make some global and local variables and see what happens. – clever_trevor Oct 13 '13 at 10:57
1

In your case, "this" is both "a reference to the current object" and "an implementation that implements the ActionListener interface". It means that the enclosing class ("this" stands by) should implement the interface ActionListener. so when someComponent is clicked (or other actions), the enclosing class will be invoked to process the event.

You can refer to below code to get the idea: "this" stands for an instance of YourClass which implements the ActionListener

public YourClass implements ActionListener
{
  private someComponent;
  public YourClass ()
   {
   someComponent = new Component();
   someComponent.addActionListener(this);
   }

  public void actionPerformed()
  {
    //add code to process the event
  }
}
danny
  • 3,046
  • 3
  • 23
  • 28
1

Here's what happens. In the example below SomeClass implements ActionListener interface which has only one method (actionPerformed which takes an ActionEvent object as an argument) that needs to be implemented. However, in order to implement this method you need an object."this" refers to an object of SomeClass.

    public class SomeClass implements ActionListener{

    SomeClass(){
    Button aButton  = new Button("Click Me");
    aButton.addActionListener(this);
    }

     public static void main(String[] args) {
       SomeClass object = new SomeClass();
     }

    public void actionPerformed(ActionEvent e) {
     //do Something when user clicks the button
    }
}
lochi
  • 860
  • 2
  • 12
  • 26
  • this refer to an object of SomeClass. However, we haven't created an object? No 'new' is used – user1232250 Oct 13 '13 at 03:14
  • 1
    I added the main method to make it clear. – lochi Oct 13 '13 at 03:20
  • 1
    @user1232250: This code will not be executed until you have created an instance of SomeClass. – Kevin Coppock Oct 13 '13 at 03:20
  • 1
    Is it clear now?? You need to create an object of SomeClass. Either within the main method or somewhere else. When you do that, the code will get executed. – lochi Oct 13 '13 at 03:23
  • How about Android, there is no main method to create new SomeClass. That is what I don't understand. Thanks. – user1232250 Oct 13 '13 at 03:29
  • 1
    I am not an android programmer. You must be using some sort of an IDE to setup your project. Maybe your IDE automatically generate an instance of SomeClass. You may need to dig deeper to find the auto generated code. I am pretty sure this answer is correct. – lochi Oct 13 '13 at 03:33