3

I know there are similar question put up related to this issue, but however, I wasn't able to resolve my issue. I've tried to simplify my problem to the following code -

class Outer
{
    Outer()
    {}

    class Inner
    {
        Inner()
        {}
    }

    void func()
    {
        System.out.println("Outer");
    }
}

public class Nested
{
    public static void main(String args[])
    {
        Outer oo = new Outer();
        Outer.Inner ii = oo.new Inner();

//          ii.func(); I know this won't work


    }
}

Can I call outer class function "func()" from object of inner class "ii"..?? If yes, how?

4 Answers4

4

Short answer: the reference to Outer.this is private in Inner so you cannot access the reference to the Outer instance from an instance of the Inner.

You can export this reference thus:

class Outer {
    Outer() {
    }

    class Inner {
        Inner() {
        }

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

    void func() {
        System.out.println("Outer");
    }
}

Then you can simply do:

ii.getOuter().func();
Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
  • Was just about to write exactly the same answer (even with the same method name) – Rohit Jain Apr 25 '15 at 10:26
  • @BoristheSpider seems like all great mind thinks same function name : Jon Skeet here same question http://stackoverflow.com/questions/1816458/getting-hold-of-the-outer-class-object-from-the-inner-class-object – Ankur Anand Apr 25 '15 at 10:41
  • @user3554962 rather than commenting with thank-yous, please consider [accepting an answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) if you question was answered. – Boris the Spider Apr 26 '15 at 19:33
1

Use Outer.this.func() from the inner class. Do note that you can only do this from the inner class, not from outside.

Distjubo
  • 959
  • 6
  • 21
  • `Outer.this` is `private` in `Inner`. You cannot access it like that. – Boris the Spider Apr 25 '15 at 10:26
  • Lol thats exactly what I mean – Distjubo Apr 25 '15 at 10:27
  • Well then it's not very helpful to the use case of the OP is it? I.E. it doesn't actually answer the question. – Boris the Spider Apr 25 '15 at 10:27
  • It answers the question. Read the last line of the question carefully. – Distjubo Apr 25 '15 at 10:29
  • The OP wants to access the method `func` on `Outer` from an instance of `Inner` in _another class_ - `Nested`. This does not answer the question, it answers a different question. – Boris the Spider Apr 25 '15 at 10:30
  • @BoristheSpider Read this quote: "Can I call outer class function "func()" from object of inner class "ii"..??" The answer is, as mentioned, no. – Distjubo Apr 25 '15 at 10:35
  • Yes, _from an object in inner class_. I have an `Inner ii`, how do I call `func()` using your method?? – Boris the Spider Apr 25 '15 at 10:40
  • There is no way to do so without providing new methods in ii. That is EXACTLY what I tried to tell you. – Distjubo Apr 27 '15 at 16:25
  • Is it? I don't see any mention of "new" or "method" in your answer. You answer just says _Use `Outer.this.func()` from the inner class._ I still insist that this answer doesn't even come close to answering the question. Maybe the comments clarify things a little, but the answer it still useless. – Boris the Spider Apr 27 '15 at 16:30
  • If you are not happy with my answer, feel free to EDIT it. – Distjubo Apr 28 '15 at 13:38
  • Also, read it again, what would you do if I say that you can only use the scope from the inner class? – Distjubo Apr 28 '15 at 13:45
1
class Outer {
   Outer() {}
   class Inner {
     Inner() {}

     void callFunc() {
       Outer.this.func();
     }
  }

  void func() {
     System.out.println("Nested");
  }
}

public class Nested {
 public static void main(String args[]) {
   Outer.Inner ii = new Outer().new Inner();
   ii.callFunc();

 // ii.func(); I know this won't work
 }
}
swinkler
  • 1,703
  • 10
  • 20
1

This will work for you

package com;


class Outer
{
    Outer()
    {}

    class Inner
    {
        Inner()
        {}
        public Outer g(){
            return Outer.this;}
    }

    void func()
    {
        System.out.println("Outer");
    }
}

public class Test1
{
    public static void main(String args[])
    {
        Outer oo = new Outer();
        Outer.Inner ii = oo.new Inner();


         ii.g().func(); 


    }
}
Ankur Anand
  • 3,873
  • 2
  • 23
  • 44