0

NOTE: I'm not talking about the way of accessing the enclosing class FROM the inner class, that is not the problem at all. What I'm talking about is the following:

class TopLevel {

    class InnerClass {
    }
}


// in some other class A.java
public void execute(InnerClass ic) {

}

Here, in the execute() method, we have the instance of the InnerClass passed as an argument. So what I actually want is to access the instance of Enclosing class, inside which the InnerClass instances that was passed to execute() was created.

How can this be achieved?

0

2 Answers 2

2

I've found a pretty good solution. If you have access to the source code, then something like this is possible:

class TopLevel {

    class InnerClass {
        // just declare the method that exposes the enclosing instance
        public TopLevel getEnclosing() {
            return TopLevel.this; 
        }   
    }
}

// in some other class A.java
public void execute(InnerClass ic) {
    ic.getEnclosing().doSomething();
}

Sign up to request clarification or add additional context in comments.

Comments

1

There is no standard way to do it.

You need to add method that returns outer class to your inner class.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.