We all use this keyword many times explisit with our instance fields or constructor, however, this refers to* current object*, for example:

class Foo imlements ActionListener{

Foo fooObject;
public Foo(){
//....
button.addActionListener(this);//this work as we pass the current object reference as an argument
//or
button.addActionListener(fooObject);// this will compile, but the action won't fire
}

@Override
public void actionPerformed(ActionEvent){
 System.out.println("Some message");
}
}

I don't understand what's current object equivelant to, what does this means in the above example?

When you call a constructor it instances(creates) an object right? When using the this keword in the object constructor it refers to the object that is being instanced(created).

When using it outside of the constructor such as in a method call like:

foo1.addDimension(dim1)

where below is the method called:

public void setDimension(Dimension dim){
    this.dim = dim;
}

the this keword references the object you used to call the method, the "current object", so this is referencing foo1.

The problem with the second statement button.addActionListener(fooObject); is that fooObject has not been created yet its just a reference(name) there is actualy nothing there so the listener is listening to empty space :).

Hope this helps.

DarkLightning7 is about 99% correct... :-)

In reality, you need to understand context. When you call a member function of an object, inside that function, "this" refers to the object in context - that the function is operating on. Inside the constructor of SomeClass, "this" is no different than inside the Someclass member function someFunction(). IE, it helps you identify the specific instance of the SomeClass you are operating upon. Confused yet? :-) This is normal.

So, think of it this way. You have a class called Car. You create an instance of a car (make or model not relevant here). You get into the car. In that case "this" refers to the car you are sitting in. Clearer yet? So, you call the Car::start() method. Inside the start() method, "this" refers to the SPECIFIC car you are starting. So...

Car myCar = new Car;
myCar.start(); // Inside start() here, "this" refers to myCar.

Still confused? Keep asking questions. We'll try to get you sorted out! :-)

also: if you want to keep the actionPerformed in the class itself, always use this, not an instance of the class. you risk getting in something like this:

public class MyFrame extends JFrame implements ActionListener{

  MyFrame sFrame = new MyFrame("other Screen");
  private JTextField field = new JTextField();
  private JButton but = new JButton("DISPLAY");
  but.addActionListener(sFrame);
  .....

  public MyFrame(){

  }

  public MyFrame(String text){
    this.field.setText(text);
  }

  @Override
  public void actionPerformed(ActionEvent event){
    System.out.println(field.getText());
  }

}

if you run a JFrame like this, the user might not even be aware that there is a second one. all he expects is that when he presses the button, he'll see the text he entered in the JTextField to be displayed, which will (almost) never be the case.

This article has been dead for over six months. Start a new discussion instead.