Access outer / inner class variables query

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

}
}
 
A

Anthony Borla

lonelyplanet999 said:
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.

Try:

...
MyOuter.MyInner.NewInner xinner = mo. new MyInner(). new NewInner();
MyOuter.MyInner.NewInner yinner = mo. new MyInner(). new NewInner();

MyOuter.MyInner.NewInner ninner = inner.new NewInner();
ninner.seeOuter();

...

Extra examples:

MyOuter.MyInner tinner = mo. new MyInner();
MyOuter.MyInner.NewInner zinner = tinner. new NewInner();

Also, try:

...
public int gety() { return y; }
...

in place of:

...
public void gety() { return y; }

...
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 ?

Try:

...
System.out.println("Outer y is "+ MyOuter.this.gety());

System.out.println("Outer y is "+ MyOuter.this.y);
System.out.println("Inner y is "+ MyOuter.this.y);
...

instead of:

...
System.out.println("Outer y is "+ MyOuter.gety());

System.out.println("Outer y is "+ MyOuter.y);
System.out.println("Inner y is "+ MyOuter.y);
...

<SNIP>

I hope this helps.

Anthony Borla
 
L

lonelyplanet999

Anthony Borla said:
Try:

...
MyOuter.MyInner.NewInner xinner = mo. new MyInner(). new NewInner();
MyOuter.MyInner.NewInner yinner = mo. new MyInner(). new NewInner();

MyOuter.MyInner.NewInner ninner = inner.new NewInner();
ninner.seeOuter();

...

Extra examples:

MyOuter.MyInner tinner = mo. new MyInner();
MyOuter.MyInner.NewInner zinner = tinner. new NewInner();

Also, try:

...
public int gety() { return y; }
...

in place of:

...
public void gety() { return y; }

...


Try:

...
System.out.println("Outer y is "+ MyOuter.this.gety());

System.out.println("Outer y is "+ MyOuter.this.y);
System.out.println("Inner y is "+ MyOuter.this.y);
...

instead of:

...
System.out.println("Outer y is "+ MyOuter.gety());

System.out.println("Outer y is "+ MyOuter.y);
System.out.println("Inner y is "+ MyOuter.y);
...

<SNIP>

I hope this helps.

Anthony Borla

Thanks! I modified the code as follow:

One thing I still couldn't figure out. If I want to access or display
members of MyInner class from MyOuter class methods (the same applied
to accessing NewInner members from MyInner class methods or MyOuter
class methods), is creating instances of MyInner (or NewInner) class
within MyOuter class the only way to achieve the goal ?

class P461 {
public static void main (String [] args) {
MyOuter mo = new MyOuter();

MyOuter.MyInner inner = mo.new MyInner();
System.out.println("inner\n=====");
inner.seeOuter();
inner.see2Outer();

MyOuter.MyInner.NewInner xinner = mo. new MyInner(). new
NewInner();
MyOuter.MyInner.NewInner ninner = inner.new NewInner();
System.out.println("xinner\n======");
xinner.seeOuter();
System.out.println("ninner\n======");
ninner.seeOuter();
}
}

class MyOuter {
private int x=7;
private int y=-1;
public void makeInner() {
MyInner in = new MyInner();
in.seeOuter();
}
public int 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(this+".see2Outer");
System.out.println("Default y is "+y);
System.out.println("Outer y is "+MyOuter.this.y);
System.out.println("Outer gety() is "+MyOuter.this.gety());
}

class NewInner {
private int x=777;
private int y=-3;
public void seeOuter() {
System.out.println("NewInner x is "+x);
}
}

}
}

Running the compiled program produced below output:

inner
=====
Outer x is 7
[email protected]
Default y is -2
Outer gety() is -1
xinner
======
NewInner x is 777
ninner
======
NewInner x is 777
 
L

lonelyplanet999

I would like to ask is it possible to add methods in MyOuter class
scope (marked by ->) to access variables or calling methods in MyInner
or NewInner classes ? :)
class MyOuter {
private int x=7;
private int y=-1;
public void makeInner() {
MyInner in = new MyInner();
in.seeOuter();
}
public int gety() { return y; }
-----> add method here
 

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,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top