How to access inner classes variables & methods from outer classes

Discussion in 'Java' started by lonelyplanet999, Nov 13, 2003.

  1. 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);
    }
    }
    }
    }
     
    lonelyplanet999, Nov 13, 2003
    #1
    1. Advertising

  2. lonelyplanet999

    VisionSet Guest

    "lonelyplanet999" <> wrote in message
    news:...
    > 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
     
    VisionSet, Nov 13, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lonelyplanet999

    Access outer / inner class variables query

    lonelyplanet999, Nov 11, 2003, in forum: Java
    Replies:
    3
    Views:
    5,307
    lonelyplanet999
    Nov 18, 2003
  2. Newbie
    Replies:
    4
    Views:
    1,398
    Newbie
    Aug 23, 2006
  3. dobest03
    Replies:
    2
    Views:
    497
    Mark P
    May 4, 2006
  4. Charles Comstock

    Why do inner methods count in outer scope?

    Charles Comstock, Mar 3, 2004, in forum: Ruby
    Replies:
    10
    Views:
    182
    Tim Bates
    Mar 4, 2004
  5. Replies:
    2
    Views:
    242
Loading...

Share This Page