Results 1 to 4 of 4
  1. #1
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Interesting static context error

    I guess I never tried the following because it gave me static context error
    which surprised me.

    Java Code:
    public class InterestingError {
       A a1 = new A();
       // the following is not allowed due to a static context error.
       static int INT1 = a1.foo;
       
       // But the following are allowed. Which to me is strange because
       // in the first case, new A() must be an instance value since it
       // can reference an instance field (e.g. foo).  Static values can't
       // do that.
       //
       // In the second case an instance field is assigned to a static field.
       // I see no difference in terms of context between line's 4 and 12.
       static A aInstance = new A();
       static int INT2 = new A().foo;
    
    }
    
    class A {
       int foo = 10;
    }
    Time to check the JLS.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Interesting static context error

    The instance field a1 is constructed only when an instance of InterestingError is constructed. IOW, it is not created when loading the class InterestingError.

    In the second case, a new A() is constructed when loading the class InterestingError.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Interesting static context error

    You meant lines 4 and 14, right?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Interesting static context error

    Yeah. I mis-corrected after I posted the code. Thanks for the explanation.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 2
    Last Post: 04-10-2013, 12:46 AM
  2. non static variable from static context error
    By chopficaro in forum New To Java
    Replies: 2
    Last Post: 06-04-2012, 01:44 PM
  3. Replies: 9
    Last Post: 06-10-2011, 12:40 AM
  4. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •