lapots
lapots

Reputation: 13395

How does reference by classname.this.method works?

For example, I've got this code:

public class NewProjectModalWindow extends Window 
{
    private void initComponents() 
    {       
        cancel = new Button("Cancel");
        cancel.addClickListener(new Button.ClickListener() 
        {
            @Override
            public void buttonClick(ClickEvent event) {
                NewProjectModalWindow.this.close();
            }
        });
    }
}

How does this work in that case? I mean - how does it understand in the anonymous class that I want to call close method for exactly this instance of NewProjectModalWindow?

How is it able to find in the bunch of objects in the memory exactly my object by using NewProjectModalWindow.this?

How does this point out to the current instance?

Upvotes: 2

Views: 109

Answers (4)

newacct
newacct

Reputation: 122439

Every instance of a non-static inner class in Java has a reference to an instance of the outer class. This reference is provided when an instance of the inner class is created. You can explicitly specify the outer-class instance when you create an instance of the inner class:

outerClassInstance.new InnerClass(...)

When you are in the scope of the outer class of this inner class, and you do not explicitly specify the outer class instance when creating the instance of the inner class (i.e. new InnerClass(...)), this is implicitly used (or SomeOuterLevelClass.this, if the immediate outer class is not the right type, where SomeOuterLevelClass is the innermost enclosing class that is the right type), as if you wrote this.new InnerClass(...).

Now, what you have in your example is an anonymous class. An anonymous class, when created in a non-static scope, is a non-static inner class, so the stuff above applies (it has a reference to an instance of the outer class), except that you cannot explicitly specify an instance of the outer class when creating an instance of an anonymous class; it is forced to be this.

Upvotes: 0

Jonathan
Jonathan

Reputation: 3644

It is essentially just a hint to the compiler / interpreter to use the reference of the outer class in the nested class / method.

You could also store the reference to (NewProjectModalWindow)this in a (final) variable and use that in the nested class / method.

Upvotes: 1

bakriawad
bakriawad

Reputation: 345

this referes to implementations, implementations are usually at the top when defining the class or within an implemented method, in your case the ActionListener.

when you work with object oriented languages you create objects, but since the object ActionListener is an implementation you use this as an object referal, and in your case, it is in an implemnted method thus the this will refer to the called method which is "ActionListener"

you can also use this when it is like this:

public class MyClass implements ActionListener  //implemented


  public MyClass()
  {
   JButton btn = new JButton("Button");
   btn.addActionListener(this);  //refers to the class - which is implementing ActionListener
  }

you may come across super as well which is refered to the extended class.

Simply put: It is a reference to the method or class that contains it.

read more about it here: How to use: this

Upvotes: 1

Eran
Eran

Reputation: 393801

NewProjectModalWindow.this refers to the enclosing instance of your anonymous inner class, so it refers to the same instance in which the Button.ClickListener instance was created.

Here's the relevant JLS entry :

15.8.4. Qualified this

Any lexically enclosing instance (§8.1.3) can be referred to by explicitly qualifying the keyword this.

Let T be the type denoted by TypeName. Let n be an integer such that T is the n'th lexically enclosing type declaration of the class or interface in which the qualified this expression appears.

The value of an expression of the form TypeName.this is the n'th lexically enclosing instance of this.

The type of the expression is T.

It is a compile-time error if the expression occurs in a class or interface which is not an inner class of class T or T itself.

Upvotes: 2

Related Questions