Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 783
fredoverflow Avatar asked Jun 30 '11 15:06

fredoverflow


People also ask

How do you access the variables of an outer class?

You can access the static variable of an outer class just using the class name.

Does outer class have access to inner?

Note: just like static attributes and methods, a static inner class does not have access to members of the outer class.

Can inner class access outer class private variables C#?

The inner class can access any non-static member that has been declared in the outer class. Scope of a nested class is limited by the scope of its (outer) enclosing class. If nothing is specified, the nested class is private (default). Any class can be inherited into another class in C# (including a nested class).

What is outer class in Java?

Nested Classes In Java, just like methods, variables of a class too can have another class as its member. Writing a class within another is allowed in Java. The class written within is called the nested class, and the class that holds the inner class is called the outer class.


2 Answers

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.

like image 190
Ted Hopp Avatar answered Sep 27 '22 22:09

Ted Hopp


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; 
        }
    }
}
like image 39
user1504714 Avatar answered Sep 27 '22 21:09

user1504714