Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access "this" reference of anonymous outer class in java

I have the following problem. Two nested anonymous types. I want to access "this" reference of the outer anonymous class inside the most inner class. Usually if one has anonymous nested class in a named outer class (lets call it "class Outer") he/she would type inside the nested class Outer.this.someMethod(). How do I refer the outer class if it's anonymous ? Example Code:

public interface Outer {
    void outerMethod();
}

public interface Inner {
    void innerMethod();
}
...
public static void main(String[] args) {
...
new Outer() {
    public void outerMethod() {
        new Inner() {
            public void innerMethod() {
                Outer.this.hashCode(); // this does not work
            } // innerMethod
        }; // Inner
    } // outerMethod
}; // Outer
...
} // main

The error I get is

No enclosing instance of the type Outer is accessible in scope

I know that I can copy the reference like this:

final Outer outerThisCopy = this;

just before instantiating the Inner object and then refer to this variable. The real goal is that I want to compare the hashCodes of outerThisCopy and the object accessed inside the new Inner object (i.e the Outer.this) for debugging purposes. I have some good arguments to think that this two objects are different (in my case). [Context: The argument is that calling a getter implemented in the "Outer" class which is not shadowed in the "Inner" class returns different objects]

Any ideas how do I access the "this" reference of the enclosing anonymous type ?

Thank you.

like image 874
egelev Avatar asked Oct 16 '14 08:10

egelev


People also ask

How do you reference an outer class in Java?

In general you use OuterClassName. this to refer to the enclosing instance of the outer class.

How do you call an anonymous class in Java?

Object = new Example() { public void display() { System. out. println("Anonymous class overrides the method display()."); } }; Here, an object of the anonymous class is created dynamically when we need to override the display() method.

How do you access the variables of an outer class?

You can access the static variable of an outer class just using the class name.

Where do we use anonymous class in Java?

Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. Use them if you need to use a local class only once.


1 Answers

You cannot access an instance of anonymous class directly from inner class or another anonymous class inside it, since the anonymous class doesn't have a name. However, you can get a reference to the outer class via a method:

new Outer()
{
    public Outer getOuter()
    {
        return this;
    }

    public void outerMethod()
    {
        new Inner()
        {
            public void innerMethod()
            {
                getOuter().hashCode();
            }
        };
    }
};
like image 200
Eng.Fouad Avatar answered Oct 15 '22 08:10

Eng.Fouad