Accessing shadowed members of an enclosing class

Discussion in 'Java' started by Bernd Fuhrmann, Oct 15, 2005.

  1. Hi!

    I was just wondering if it is possible to access shadowed variables from
    within an inner class. Example:

    public class Outer {
    public int a;//#1
    public class Inner {
    public int a;//#2
    public void dosth() {
    System.out.println("a from inner is "+a);
    // Is there any way to access a from Outer
    // within this context?
    }
    }
    }

    Is there any way to access the member a of Outer within Inner?

    Thanks in advance,
    Bernd Fuhrmann
     
    Bernd Fuhrmann, Oct 15, 2005
    #1
    1. Advertising

  2. Bernd Fuhrmann

    Stefan Ram Guest

    Bernd Fuhrmann <> writes:
    >Is there any way to access the member a of Outer within Inner?


    Outer.this.a
     
    Stefan Ram, Oct 15, 2005
    #2
    1. Advertising

  3. Stefan Ram wrote:
    > Bernd Fuhrmann <> writes:
    >> Is there any way to access the member a of Outer within Inner?

    >
    > Outer.this.a
    >

    Thanks says
    Bernd Fuhrmann
     
    Bernd Fuhrmann, Oct 15, 2005
    #3
  4. Bernd Fuhrmann

    Roedy Green Guest

    On Sat, 15 Oct 2005 21:40:13 +0200, Bernd Fuhrmann
    <> wrote or quoted :

    >public class Outer {
    > public int a;//#1
    > public class Inner {
    > public int a;//#2
    > public void dosth() {
    > System.out.println("a from inner is "+a);
    > // Is there any way to access a from Outer
    > // within this context?
    > }
    > }


    In general you can access nested elements with Outer. and Inner.
    e.g.Outer.this Inner.a


    See http://mindprod.com/jgloss/nestedclasses.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Oct 15, 2005
    #4
    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. Tor Iver Wilhelmsen
    Replies:
    0
    Views:
    424
    Tor Iver Wilhelmsen
    Jul 18, 2003
  2. Jayaram
    Replies:
    0
    Views:
    446
    Jayaram
    Jul 18, 2003
  3. Replies:
    0
    Views:
    1,134
  4. Greg Brown
    Replies:
    1
    Views:
    397
    Andrew Thompson
    Dec 16, 2004
  5. Brad Clements

    UnboundLocalError on shadowed import

    Brad Clements, Jun 29, 2004, in forum: Python
    Replies:
    3
    Views:
    436
    Peter Hansen
    Jun 30, 2004
Loading...

Share This Page