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

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

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

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. Replies:
    0
    Views:
    1,212
  2. CoolPint
    Replies:
    8
    Views:
    1,247
    Jeff Schwab
    Dec 14, 2003
  3. Stanimir Stamenkov
    Replies:
    4
    Views:
    640
    Chris Uppal
    Jul 14, 2006
  4. hdixon
    Replies:
    3
    Views:
    864
    hdixon
    Jul 9, 2006
  5. Jonathan
    Replies:
    3
    Views:
    585
  6. bugbear
    Replies:
    4
    Views:
    639
    bugbear
    Aug 23, 2006
  7. Bhawna
    Replies:
    7
    Views:
    642
    Bhawna
    Aug 26, 2008
  8. John Lam
    Replies:
    12
    Views:
    385
    Rick DeNatale
    Aug 31, 2006
Loading...