referring to static fields of enclosing class from static inner class?

B

bugbear

Is there a way to name the "outer" class?
I understand the Outer is NOT a super-class
of inner. Is inner "almost" stand alone
or is there an eaqsy way (other than Outer.a)
that the method f() can refer to the
string a from Outer?

BugBear


class Outer {
public static final String a = "a";
static class inner {
String f() {
return a; // doesn't work.
return super.a; // doesn't work
return Outer.a; // works but is not what I want
}
}

}
 
B

bugbear

bugbear said:
Is there a way to name the "outer" class?
I understand the Outer is NOT a super-class
of inner. Is inner "almost" stand alone
or is there an eaqsy way (other than Outer.a)
that the method f() can refer to the
string a from Outer?

BugBear


class Outer {
public static final String a = "a";
static class inner {
String f() {
return a; // doesn't work.
return super.a; // doesn't work
return Outer.a; // works but is not what I want
}
}

}

Colour me an idiot. "return a;" DOES work.

BugBear
 
M

Mike Schilling

bugbear said:
Colour me an idiot. "return a;" DOES work.

It will work if there's no "closer" a (parameter, local variable, field in
inner) obscuring it. Suppose something was obscuring the static field; can
you expalin why "Outer.a" isn't the "right" way to refer to it?
 
B

bugbear

Mike said:
It will work if there's no "closer" a (parameter, local variable, field in
inner) obscuring it. Suppose something was obscuring the static field; can
you expalin why "Outer.a" isn't the "right" way to refer to it?

In my instance because the out class had a long name,
and I wanted to ue "rather a lot" of fields from
it.

Just typing convenience and line length.

Or (if you will) ease and elegance of expression.

BugBear
 

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

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top