referring to static fields of enclosing class from static inner class?

Discussion in 'Java' started by bugbear, Aug 22, 2006.

  1. bugbear

    bugbear Guest

    Is there a way to name the "outer" class?
    I understand the Outer is NOT a super-class
    of inner. Is inner "almost" stand alone
    or is there an eaqsy way (other than Outer.a)
    that the method f() can refer to the
    string a from Outer?

    BugBear


    class Outer {
    public static final String a = "a";
    static class inner {
    String f() {
    return a; // doesn't work.
    return super.a; // doesn't work
    return Outer.a; // works but is not what I want
    }
    }

    }
     
    bugbear, Aug 22, 2006
    #1
    1. Advertising

  2. bugbear

    bugbear Guest

    Re: referring to static fields of enclosing class from static innerclass?

    bugbear wrote:
    > Is there a way to name the "outer" class?
    > I understand the Outer is NOT a super-class
    > of inner. Is inner "almost" stand alone
    > or is there an eaqsy way (other than Outer.a)
    > that the method f() can refer to the
    > string a from Outer?
    >
    > BugBear
    >
    >
    > class Outer {
    > public static final String a = "a";
    > static class inner {
    > String f() {
    > return a; // doesn't work.
    > return super.a; // doesn't work
    > return Outer.a; // works but is not what I want
    > }
    > }
    >
    > }


    Colour me an idiot. "return a;" DOES work.

    BugBear
     
    bugbear, Aug 22, 2006
    #2
    1. Advertising

  3. Re: referring to static fields of enclosing class from static innerclass?

    bugbear wrote:
    > Colour me an idiot.


    OK - where is the paint? ;-)

    SCNR,
    Ingo
     
    Ingo R. Homann, Aug 22, 2006
    #3
  4. "bugbear" <bugbear@trim_papermule.co.uk_trim> wrote in message
    news:44eaf94e$0$2694$...
    > bugbear wrote:
    >> Is there a way to name the "outer" class?
    >> I understand the Outer is NOT a super-class
    >> of inner. Is inner "almost" stand alone
    >> or is there an eaqsy way (other than Outer.a)
    >> that the method f() can refer to the
    >> string a from Outer?
    >>
    >> BugBear
    >>
    >>
    >> class Outer {
    >> public static final String a = "a";
    >> static class inner {
    >> String f() {
    >> return a; // doesn't work.
    >> return super.a; // doesn't work
    >> return Outer.a; // works but is not what I want
    >> }
    >> }
    >>
    >> }

    >
    > Colour me an idiot. "return a;" DOES work.


    It will work if there's no "closer" a (parameter, local variable, field in
    inner) obscuring it. Suppose something was obscuring the static field; can
    you expalin why "Outer.a" isn't the "right" way to refer to it?
     
    Mike Schilling, Aug 22, 2006
    #4
  5. bugbear

    bugbear Guest

    Re: referring to static fields of enclosing class from static innerclass?

    Mike Schilling wrote:
    >
    >
    > It will work if there's no "closer" a (parameter, local variable, field in
    > inner) obscuring it. Suppose something was obscuring the static field; can
    > you expalin why "Outer.a" isn't the "right" way to refer to it?


    In my instance because the out class had a long name,
    and I wanted to ue "rather a lot" of fields from
    it.

    Just typing convenience and line length.

    Or (if you will) ease and elegance of expression.

    BugBear
     
    bugbear, Aug 23, 2006
    #5
    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:
    435
    Tor Iver Wilhelmsen
    Jul 18, 2003
  2. Jayaram
    Replies:
    0
    Views:
    456
    Jayaram
    Jul 18, 2003
  3. Stanimir Stamenkov
    Replies:
    4
    Views:
    518
    Chris Uppal
    Jul 14, 2006
  4. Jonathan
    Replies:
    3
    Views:
    443
  5. Richard Everhart

    Referring to method in enclosing class

    Richard Everhart, Jan 31, 2008, in forum: Ruby
    Replies:
    5
    Views:
    139
    Phrogz
    Jan 31, 2008
Loading...

Share This Page