Getting a reference to enclosing instance without using literal classname

R

Robert Dodier

Hello,

Given something like class Outer { class Inner {...} ... } and an
instance of Outer.Inner.

Is there any way to retrieve a reference to the enclosing instance
without mentioning the class name "Outer" ??

I know that Class.getEnclosingClass can find the enclosing class, but
there
doesn't seem to be a similar object method, say,
"Object.getEnclosingInstance".

I am trying to process multiple, undetermined classes in a uniform
way,
so if possible I want to avoid peppering the code with literal class
names.

Thanks for any information.

Robert Dodier
 
R

Roedy Green

I am trying to process multiple, undetermined classes in a uniform
way,
so if possible I want to avoid peppering the code with literal class
names.

I once proposed there should be a "that" keyword corresponding to
"this" to mean the current class.
 
M

Mark Space

Robert said:
Hello,

Given something like class Outer { class Inner {...} ... } and an
instance of Outer.Inner.

Is there any way to retrieve a reference to the enclosing instance
without mentioning the class name "Outer" ??

If you think about it... an inner class is just a concept maintained by
the compiler, so

class Outer {
class Inner {}
}

is really just:

class Outer {}

class Outer$$Inner {
private Outer that;
}

Where the private field is just used to maintain the illusion of a
connection. So my first thought is if it's private, you really ought to
make a public getter.

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

But my second thought was that if it's just a field... you could
probably get at it with reflection somehow. This could be very brittle
since not all Java compilers will use the same naming convention, but it
might get you out of whatever jam you are in for now, assuming that the
public getter is not an option.

(I have no idea what this field is actually named. You'll have to build
a little test case to discover what your compiler does.)
 
R

Robert Dodier

But my second thought was that if it's just a field... you could
probably get at it with reflection somehow.

Thanks for the hint. Looks like javac names it "this$0".

However, since that field is private, access via reflection
from outside the class is prohibited, isn't it?

best,

Robert Dodier
 
M

Mark Space

Robert said:
Thanks for the hint. Looks like javac names it "this$0".

However, since that field is private, access via reflection
from outside the class is prohibited, isn't it?

As far as I know, yeah you can't get at it. I've heard rumor that
there's a way around this, but I don't know what the technique is.
 
E

EJP

Robert said:
However, since that field is private, access via reflection
from outside the class is prohibited, isn't it?

From outside the outer class, yes, but see Field.setAccessible().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top