Getting Hold of the Outer Class Object from the Inner Class Object

Getting hold of the outer class object from the inner class object

Within the inner class itself, you can use OuterClass.this. This expression, which allows to refer to any lexically enclosing instance, is described in the JLS as Qualified this.

I don't think there's a way to get the instance from outside the code of the inner class though. Of course, you can always introduce your own property:

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

EDIT: By experimentation, it looks like the field holding the reference to the outer class has package level access - at least with the JDK I'm using.

EDIT: The name used (this$0) is actually valid in Java, although the JLS discourages its use:

The $ character should be used only in
mechanically generated source code or,
rarely, to access pre-existing names on
legacy systems.

Get the outer class object from an inner class object

You are looking for the Class.getDeclaringClass() method:

public Class getDeclaringClass()

If the class or interface represented by this Class object is a member of
another class, returns the Class object representing the class in which it
was declared. This method returns null if this class or interface is not a
member of any other class. If this Class object represents an array class,
a primitive type, or void,then this method returns null.

Returns: the declaring class for this class

Outer class object reference when Inner class object is created outside the Outer class

Inner classes can be instantiated outside the outer class using Outer_class.inner_class.

No they can't. They are created via:

outer.new Outer.Inner(...)

where outer is a primary-expression that evaluates to a reference to an instance of Outer, for example new Outer(...).

or

this.new Inner(...)

if you are inside Outer, or for short

new Inner(...)

if you are inside Outer.

But the question arises what happens to Outer class object? means there is no reference variable referencing to it.

Oh yes there is: see above.

Obviously it will be on the heap till the inner class object is on heap because Inner classes feed on outer class. So what mechanism java uses in such situations?

Your question is founded on a mistake.

Accessing outer-class fields through inner-class instance from outer-class

Why can't I access the outer classes field through an instance of
inner class in outer class in line 8?

Because the field is a field of the class OuterClass and not of the class InnerClass. So to access it, you need an instance of the class OuterClass, not of the class InnerClass.

Sure, inside InnerClass definition, you can implicitly access all the fields of OuterClass. But that's only a matter of access from inside this context. You're not specifying what is the object of which you're trying to access the field, so the language automatically selects that for you. It's usually this.field, but in the case of a field from the containing class, it's actually OuterClass.this.field.

Once you're trying to indicate what is the object of which you're trying to access a field, rather than let the language implicitly select that object for you, well this object must actually be of the class that contains the field.

Access outer class function from Object of Inner Class

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();

How to access the outer class object from the inner class in c++

You need to initialize member references in the constructor's initialization list. And do the same for the dynIte member: initialize it in outer's constructor.

Something like this:

class Outer {
class Inner {
int stuff;
Outer &outer;

public:
Inner(Outer &o): outer(o) {
// Warning: outer is not fully constructed yet
// don't use it in here
std::cout << "Inner: " << this << std::endl;
};
};

int things;
Inner inner;

public:
Outer(): inner(*this) {
std::cout << "Outer: " << this << std::endl;
}
};


Related Topics



Leave a reply



Submit