L
lonelyplanet999
Hi,
I have below code to test accessing outer & inner class members from
within inner class.
I met below problems (see the commented code for illustration).
1. I couldn't create instance of NewInner class from MyOuter class
instance mo. Both commented instantiation code resulted in compiler
complaining cannot resolve symbol MyOuter.
2. When I tried to access private variable y defined in classes
MyOuter & NewInner from within class MyInner (see the commented
System.out.println statements), compiler complained that y (or the
gety() method) is non-static & cannot be referenced from a static
context. I would like to ask in this case how can I change to code to
make accessing those 'y's possible from within methods of MyInner ?
Tks :>
class P461 {
public static void main (String [] args) {
MyOuter mo = new MyOuter();
MyOuter.MyInner inner = mo.new MyInner();
inner.seeOuter();
// MyOuter.MyInner.NewInner xinner = mo.MyInner().new NewInner();
// MyOuter.MyInner.NewInner yinner = mo.MyInner.new NewInner();
MyOuter.MyInner.NewInner ninner = inner.new NewInner();
ninner.seeOuter();
}
}
class MyOuter {
private int x=7;
private int y=-1;
public void makeInner() {
MyInner in = new MyInner();
in.seeOuter();
}
public void gety() { return y; }
class MyInner {
private int y=-2;
public void seeOuter() {
System.out.println("Outer x is "+x);
}
public void see2Outer() {
System.out.println("Default y is "+y);
// System.out.println("Outer y is "+MyOuter.gety());
// System.out.println("Outer y is "+MyOuter.y);
// System.out.println("Inner y is "+NewInner.y);
}
class NewInner {
private int x=777;
private int y=-3;
public void seeOuter() {
System.out.println("NewInner x is "+x);
}
}
}
}
I have below code to test accessing outer & inner class members from
within inner class.
I met below problems (see the commented code for illustration).
1. I couldn't create instance of NewInner class from MyOuter class
instance mo. Both commented instantiation code resulted in compiler
complaining cannot resolve symbol MyOuter.
2. When I tried to access private variable y defined in classes
MyOuter & NewInner from within class MyInner (see the commented
System.out.println statements), compiler complained that y (or the
gety() method) is non-static & cannot be referenced from a static
context. I would like to ask in this case how can I change to code to
make accessing those 'y's possible from within methods of MyInner ?
Tks :>
class P461 {
public static void main (String [] args) {
MyOuter mo = new MyOuter();
MyOuter.MyInner inner = mo.new MyInner();
inner.seeOuter();
// MyOuter.MyInner.NewInner xinner = mo.MyInner().new NewInner();
// MyOuter.MyInner.NewInner yinner = mo.MyInner.new NewInner();
MyOuter.MyInner.NewInner ninner = inner.new NewInner();
ninner.seeOuter();
}
}
class MyOuter {
private int x=7;
private int y=-1;
public void makeInner() {
MyInner in = new MyInner();
in.seeOuter();
}
public void gety() { return y; }
class MyInner {
private int y=-2;
public void seeOuter() {
System.out.println("Outer x is "+x);
}
public void see2Outer() {
System.out.println("Default y is "+y);
// System.out.println("Outer y is "+MyOuter.gety());
// System.out.println("Outer y is "+MyOuter.y);
// System.out.println("Inner y is "+NewInner.y);
}
class NewInner {
private int x=777;
private int y=-3;
public void seeOuter() {
System.out.println("NewInner x is "+x);
}
}
}
}