开发者

Accessing field of outer class

How do I access a field of an outer class, given a reference to an object of the inner class?

class Outer
{
    int field;

    class Inner
    {
        void method(Inner parameter)
        {
            // working on the current instance is easy :)
            field = 0;
            Outer.this.field = 1;

            // working on another instance is hard :(
            parameter.field = 2;              // does not compile
            parameter.Outer.this.field = 3;   // does not compile
            parameter.outer().field = 4;      // This works...
        }

        // ...but I really don't want to have to write this method!
        Outer outer()
        {
            return Outer.this;
        }
    }
}
开发者_如何学运维

I also tried Outer.parameter.field and many other variants. Is there a syntax that does what I want?


How about this solution:

class Outer
{
    int field;

    class Inner
    {
        final Outer outer = Outer.this;
        void method(Inner parameter)
        {
            // working on the current instance is easy :)
            field = 0;
            Outer.this.field = 1;

            // working on another instance:
            parameter.outer.field = 2; 
        }
    }
}


From outside the inner class, I believe that there's no way, given a reference to an inner class instance, to reference members of the outer class instance. From inside a non-static inner class, you can, of course, reference them using the Outer.this.* syntax.

Think of it this way: the inner class is actually a completely separate class. It has a compiler-generated field (usually named something weird like this$0). Within the inner class, the language allows you to reference that field using Outer.this; however, that syntactic sugar is not available outside the inner class itself. Neither is the compiler-generated field.


There are two ways to look at what you're doing, as a static method or as a non-static method.

At first glance, your example looks more like a static method, since it is manipulating the state of the parameter rather than its own state. The static problem has been covered before, e.g. see In Java, how do I access the outer class when I'm not in the inner class?

But your example is of a non-static method, and I think I see what you're getting and why you think that an outer reference should be possible. After all, there are other situations in which you can access implementation details of other instances of your own type -- for example, it's common to reference the input parameter's private member fields when overriding equals. Unfortunately I just don't think that java provides a way to refer to another's lexically enclosing instance. This probably has something to do with the way that java actually implements non-static inner classes. Java uses this$0 for its own purposes, but you do not have access to this synthetic field.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜