Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to access inner classes variables & methods from outer classes

Reply
Thread Tools

How to access inner classes variables & methods from outer classes

 
 
lonelyplanet999
Guest
Posts: n/a
 
      11-13-2003
Hi,

I have below code defining 1 outer class enclosing 2 inner classes.

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);
}
}
}
}
 
Reply With Quote
 
 
 
 
VisionSet
Guest
Posts: n/a
 
      11-13-2003

"lonelyplanet999" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> Hi,
>
> I have below code defining 1 outer class enclosing 2 inner classes.
>
> 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 ?

....

Yes, as with any instance attribute of any class, you must have an instance
to go with it, it only exists when you create an instance.

--
Mike W


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT Access outer loop from inner Newbie XML 4 08-23-2006 08:15 PM
how to access the outer structure member variable from inner struture? dobest03 C++ 2 05-04-2006 09:20 AM
Why do inner methods count in outer scope? Charles Comstock Ruby 10 03-04-2004 03:45 AM
Access outer / inner class variables query lonelyplanet999 Java 3 11-18-2003 07:16 PM
inner classes in python as inner classes in Java Carlo v. Dango Python 14 10-19-2003 08:49 AM



Advertisments