1

I have a class structure like this:

public class OuterClass {

    private static class InnerClass {

        public void someMethod() {
            OtherClass.otherMethod(<???>);
        }

}

which refers to a static method of some other class OtherClass:

public class OtherClass {

    public static void otherMethod(OuterClass) {
        ....
    }

}

I am trying to figure out what to put in place of the <???>. How do I refer to the instance of the outer class from within the inner static class? What I would like to do is to refer to the implicit this of the OuterClass.

fiveclubs
  • 2,392
  • 2
  • 18
  • 28
  • By not making the inner class `static`? – tobias_k Aug 05 '15 at 14:41
  • @tobias_k, sure I agree this is not an ideal class setup. But let's say I can't change it. – fiveclubs Aug 05 '15 at 14:41
  • Which instance of the outer class? – Sotirios Delimanolis Aug 05 '15 at 14:43
  • You have a nested class, not an inner class. – Sotirios Delimanolis Aug 05 '15 at 14:43
  • 2
    If the class is `static`, there is no *the* instance of the outer class, you have to pass one explicitly. If you have 100 instances of `OuterClass` (or none at all), and you invoke that method of `InnerClass` from some totally unrelated place, which instance should it use? – tobias_k Aug 05 '15 at 14:44
  • 1
    As your class is nested (static) there is no one relation between outer class instance and this class, It's just a type declaration - nothing more. You should treat it as the ordinar independent class - or make you inner class non-static. – Dmitry Nikiforov Aug 05 '15 at 14:46
  • `This ultimately was the answer I was looking for.`---unfortunately, you didn't _ask_ for it and the duplicate is a match. – Marko Topolnik Aug 05 '15 at 14:59
  • @MarkoTopolnik, I apologize for not asking the question the right way. It was not until some of the answers started coming in that I realized how important the nature of the class definitions were (i.e. static vs. non-static). The answers helped me realize this, and my problem is solved. I tried to capture this in the re-wording of the question, but if the community deems it unacceptable, I can live with that. Thanks for the help. – fiveclubs Aug 05 '15 at 15:10
  • 1
    The duplicate answers your question perfectly: it explains the importance of the difference between a static and non-static nested class. You have also got your answer here, which you accepted---so I see only benefit from the duplicate designation. Don't assume that being closed as a duplicate is a hat of shame. Once I even closed my own question as such---the point is reducing noise and linking related questions together. – Marko Topolnik Aug 05 '15 at 15:14

2 Answers2

4

You obviously need an object of OuterClass type:

public void someMethod() {
    OuterClass oc = new OuterClass();
    OtherClass.otherMethod(oc);
}

In case that your inner class is not static, then you could do:

//remove static here
private class InnerClass { 
    public void someMethod() {
        OtherClass.otherMethod(OuterClass.this);
    }
}

You should know the different between nested classes - static and non static. Static nested classes are simply classes like every other, just defined within other class (usually because of encapsulation principle). Inner static class instances have no knowledge of outer class instance.

Nested inner classes (non static) mandate that an object of the inner class exist within an instance of the outer class. That's why you can access it via OuterClass.this.

darijan
  • 9,725
  • 25
  • 38
  • This is what I wanted to do. Forgetting that it makes no sense for the inside static class to refer to 'this' in any capacity, I was able to make it non-static, and refer to OuterClass.this. This was the syntax I didn't know you could do. Thanks. – fiveclubs Aug 05 '15 at 14:51
  • "Inner static class" is a misnomer -- an *inner class* is a **nested class** that is **not** `static`. – Jan Nielsen Feb 22 '17 at 13:31
0

The simpliest way is to pass an instance of the outerClass in the constructor or in the method since the innerClass don't know this class.

like this:

public void someMethod(OuterClass outerClass) {
   OtherClass.otherMethod(outerClass.myMethod());
}
JFPicard
  • 5,029
  • 3
  • 19
  • 43