0

uQty.addFocusListener(this);

I am working an example from Object Oriented Design in java, and am perplexed about the use of this as a parameter for the addFocusListener method. I have done an extensive online search for an explanation, but all I get are more examples with no explanation. Does this refer to the field or the object that the field is part of?

Tom Magaro
  • 181
  • 3
  • 13

1 Answers1

0

this refers to the instance of the object that it is used in. So when using uQty.addFocusListener(this), it would be in a class that implements FocusListener.

For instance (For this example I'm going to assume uQty is a JTextField):

public class Foo() implements FocusListener {
    public Foo() {
        JTextField uQty = new JTextField("A TextField");
        uQty.addFocusListener(this);
    }
}

In this example, this refers to the class Foo, and as the class implements FocusListener it can be added to an object as one.

Joshua Burt
  • 76
  • 1
  • 7