Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By jim829

Thread: Can't have static fields inside inner classes, unless they're constants. Why?

  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Can't have static fields inside inner classes, unless they're constants. Why?

    We can't declare a static field inside a nonstatic-nested/inner class. That is understandable. It is because inner classes are essentially enclosing class' instance-members, i.e. each instance of the enclosing class has its own copy of the inner class. 'Static' implies that there is only one copy of something which is available to every instance. So it doesn't make sense to declare a static field inside an inner class.

    But the question is that how does making that field a constant help that?

    Why are constant static fields allowed in Inner classes?

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

    Default Re: Can't have static fields inside inner classes, unless they're constants. Why?

    Consider the following:

    Java Code:
    class Foo {
        class Bar {
           static int val = 1;
        }
    }
    
    Foo f1 = new Foo();
    Bar b11 = f1.new Bar();
    Bar b12 = f1.new Bar();
    Foo f2 = new Foo();
    Bar b21 = f2.new Bar();
    Bar b22 = f2.new Bar();
    Now you could do something like this b12.val = 4.
    But then b21 and b22 would see val =4 also. But they are supposed to be tied to
    a different instance of Foo. So you have some inconsistency. Making it final
    allows it to be shared without creating this inconsistency. This is just a guess
    on my part.

    Now the question is, why didn't the designers tighten the scope of the non-final static field
    in the inner class so that this cross pollination couldn't happen. I don't know. Perhaps
    they thought it was too difficult vs value gained. Maybe someone else on this forum
    has a better answer.

    Regards,
    Jim
    Last edited by jim829; 05-16-2016 at 04:43 AM.
    Zarah likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Re: Can't have static fields inside inner classes, unless they're constants. Why?

    why didn't the designers tighten the scope of the non-final static field
    in the inner class so that this cross pollination couldn't happen.
    What is meant by tightening their scope? How would they do that?

  4. #4
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Re: Can't have static fields inside inner classes, unless they're constants. Why?

    We say that nested classes are members of the enclosing classes. Every instance of a class has its own copy of every nonstatic member of the class.

    An instance of Bar is bound to the instance of Foo which is used to create it. When f1 and f2 are two different instances of Foo, and when we think of Bar as a member of the enclosing class Foo, f1 and f2 should have their own copies of Bar. Bar for f1 and Bar for f2 should behave as separate entities, no? If yes, then every instance of Bar associated with f1 should have only one copy of any static field of Bar; and every instance of Bar associated with f2 should have one copy of any static field of Bar. But the copy associated with f1's Bar should be different to the copy associated with f2's Bar. Isn't it?

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

    Default Re: Can't have static fields inside inner classes, unless they're constants. Why?

    Quote Originally Posted by Zarah View Post
    What is meant by tightening their scope?
    It is my own term. I simply meant enforce non-final statics in non-static inner classes
    to be inner-class scoped per outer class instance.

    How would they do that?
    I don't know. Perhaps the same way they keep local variables scoped differently from instance
    fields (even if the names are the same). It is part of compiler/loader design; different symbol
    tables (perhaps), etc. And to do so may create other problems I haven't thought of.

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

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Can't have static fields inside inner classes, unless they're constants. Why?

    Quote Originally Posted by Zarah View Post
    But the question is that how does making that field a constant help that?

    Why are constant static fields allowed in Inner classes?
    It helps because they can't be changed, so a single instance of it can exist happily, unlike a static variable, which would not for the reasons you mention.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 2
    Last Post: 05-14-2016, 11:45 PM
  2. Static fields
    By riverhare in forum New To Java
    Replies: 2
    Last Post: 05-30-2013, 07:57 AM
  3. Can non-static Inner Class have static fields ?
    By fatabass in forum New To Java
    Replies: 3
    Last Post: 02-05-2012, 09:47 PM
  4. Replies: 3
    Last Post: 12-27-2011, 04:17 AM
  5. Static fields
    By Eranga in forum Advanced Java
    Replies: 25
    Last Post: 08-29-2009, 11:06 AM

Posting Permissions

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