Re: Enum basics
John B. Matthews wrote:
Lew wrote:
....
Boom. Second question answered. No, an enum cannot access an
enclosing class's instance members. What's the big mystery?
D'oh, I misread the question, thinking of _static_ members of the
enclosing class; Roedy clearly said _instance_.
Yeah, I had to reread the post a couple of times to make sure I had the sen=
se of it right way 'round.
As for my post upthread, the "you" there was the generalized "you, the read=
er", not you the poster. I was writing a post, not an email, so what I sai=
d was not meant for anyone to take personally but in the spirit of an essay=
..
Just in case, you know.
As for the distinction between "nested" (i.e., static) and "inner" (instanc=
e) classes, I have been much more sensitive to the fine distinction after e=
ncountering the interesting edge case of an inner class that inherits from =
its containing class and accesses the latter's private members.
package eegee;
public class Outer
{
private String foo()
{
return "Outer.foo() ";
}
protected String bar()
{
return "Outer.bar() ";
}
class Inner extends Outer
{
public String foo()
{
return "Inner.foo() ";
}
public String bar()
{
return "Inner.bar() ";
}
}
public static void main(String [] args)
{
Outer encloser = new Outer();
Inner inner = encloser.new Inner();
Outer outer = inner;
System.out.println("encloser: " + encloser.foo() + encloser.bar());
System.out.println(" outer: " + outer.foo() + outer.bar());
System.out.println(" inner: " + inner.foo() + inner.bar());
}
}
Of course, @Override helps here.
--
Lew