Static Access Discrepancy?

Discussion in 'Java' started by Paul Carey, Jan 16, 2004.

  1. Paul Carey

    Paul Carey Guest

    Hi
    I just noticed the following when studying for the scjp. Does anyone
    know if there's any reason for the compiler errors generated by the
    commented line below as to me it doesn't seem consistent with the
    other ways of accessing static variables and methods.
    Thanks
    Paul

    public class StaticAccess
    {
    static int i = 0;

    static int method()
    {
    return 5;
    }

    static class StaticNested
    {
    int nestedMethod()
    {
    return 10;
    }
    }

    void testAccess()
    {
    int a = StaticAccess.i;
    int b = this.i;

    int c = StaticAccess.method();
    int d = this.method();

    StaticNested sna = new StaticAccess.StaticNested();
    // StaticNested snb = new this.StaticNested();

    StaticNested snc = new StaticNested();
    }
    }
     
    Paul Carey, Jan 16, 2004
    #1
    1. Advertising

  2. Paul Carey

    Jon Skeet Guest

    Paul Carey <> wrote:
    > I just noticed the following when studying for the scjp. Does anyone
    > know if there's any reason for the compiler errors generated by the
    > commented line below as to me it doesn't seem consistent with the
    > other ways of accessing static variables and methods.


    Indeed, it's not. Out of all the various bits of horrible syntax for
    nested classes, this is the one bit which is not only good, but better
    than the rest of Java.

    I would recommend never accessing static members with a reference type
    expression - it only leads to misleading code such as
    Thread.currentThread.sleep(...).

    --
    Jon Skeet - <>
    http://www.pobox.com/~skeet
    If replying to the group, please do not mail me too
     
    Jon Skeet, Jan 16, 2004
    #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. Anand P Paralkar

    Discrepancy in CLB Usage Report

    Anand P Paralkar, Jul 2, 2003, in forum: VHDL
    Replies:
    0
    Views:
    1,052
    Anand P Paralkar
    Jul 2, 2003
  2. Gopal
    Replies:
    4
    Views:
    395
    Dario
    Oct 30, 2003
  3. Eric
    Replies:
    0
    Views:
    385
  4. Pr
    Replies:
    1
    Views:
    372
    Ron Natalie
    Jan 8, 2004
  5. Robert Brewer

    os.path.exists discrepancy

    Robert Brewer, Nov 15, 2004, in forum: Python
    Replies:
    0
    Views:
    320
    Robert Brewer
    Nov 15, 2004
Loading...

Share This Page