In Java, how do I access the outer class when I

2019-01-08 15:19发布

If I have an instance of an inner class, how can I access the outer class from code that is not in the inner class? I know that within the inner class, I can use Outer.this to get the outer class, but I can't find any external way of getting this.

For example:

public class Outer {
  public static void foo(Inner inner) {
    //Question: How could I write the following line without
    //  having to create the getOuter() method?
    System.out.println("The outer class is: " + inner.getOuter());
  }
  public class Inner {
    public Outer getOuter() { return Outer.this; }
  }
}

8条回答
劫难
2楼-- · 2019-01-08 15:32

Where is the problem for simply write this?

class OuterClass{
class InnerClass{
    OuterClass outerClass = OuterClass.this;
}

}

查看更多
Explosion°爆炸
3楼-- · 2019-01-08 15:38

The bytecode of the Outer$Inner class will contain a package-scoped field named this$0 of type Outer. That's how non-static inner classes are implemented in Java, because at bytecode level there is no concept of an inner class.

You should be able to read that field using reflection, if you really want to. I have never had any need to do it, so it would be best for you to change the design so that it's not needed.

Here is how your example code would look like when using reflection. Man, that's ugly. ;)

public class Outer {
    public static void foo(Inner inner) {
        try {
            Field this$0 = inner.getClass().getDeclaredField("this$0");
            Outer outer = (Outer) this$0.get(inner);
            System.out.println("The outer class is: " + outer);

        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public class Inner {
    }

    public void callFoo() {
        // The constructor of Inner must be called in 
        // non-static context, inside Outer.
        foo(new Inner()); 
    }

    public static void main(String[] args) {
        new Outer().callFoo();
    }
}
查看更多
女痞
4楼-- · 2019-01-08 15:40

Can't you just do something like this:

public static class Inner { // static now
    private final Outer parent;

    public Inner(Outer parent) { this.parent = parent; }

    public Outer get Outer() { return parent; }
}
查看更多
倾城 Initia
5楼-- · 2019-01-08 15:41

Here is one reason you may want this behavior: You have the inner class instance and need to access a method defined in the outer class using reflection.

For the record, "inner.getClass().getDeclaredField("this$0")"works. Since the field is private, you also need to call field.setAccessible(true).

查看更多
劫难
6楼-- · 2019-01-08 15:42

What's wrong with adding a getter when you need to access the outer class? That way you can control whether access is allowed or not.

查看更多
Summer. ? 凉城
7楼-- · 2019-01-08 15:43

This, in fact, is a very good question if, for example, you need to be able to check if two distinct instances of an Innner class share the same instance of Outer class (== or equals depending on the context).

I'd suggest to make a general purpose interface (not absolutely required for named inner classes but can be "instancedof"/casted to):

public interface InnerClass<Outer> {
    Outer getOuter();
}

that can be applied to any named inner class.

Then you do something like:

class MyInnerClass implements InnerClass<Outer> {
    Outer getOuter() {
        return Outer.this;
    }
    // remaining implementation details
}

and this way you can access outer class from any inner class implementing InnerClass<Outer> interface (and check it actually implements it).

If your inner class is anonymous, you can only do (thanks to Rich MacDonald for its sample):

new InterfaceOrAbstractClass<Outer>() {
    Outer getOuter() { // super inefficient but this is the only way !
        return (Outer)getClass().getDeclaredField("this$0");
    }
    /* other methods */
}

but InterfaceOrAbstractClass must implements InnerClass<Outer> to be able to access getOuter() outside of the anonymous class body!

It would be so much easier if javac automatically implemented some kind of InnerClass<Outer> interface on ALL inner classes, and it could do that super efficiently even on anonymous classes (no sluggish introspection processing)!

查看更多
登录 后发表回答