Results 1 to 7 of 7
  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Why can't we have a static (and non final) field in a non-static inner class?

    I tried to declare a static (and non-final) field inside a non-static inner class, and the compiler complains that the field can not be declared static in a non-static inner type, unless initialized with a constant expression.

    Eclipse offers three quick fixes:
    1. Remove the modifier 'static' from the field declaration.
    2. Change the field's modifier to 'static final'
    3. Add 'static' modifier to the parent type

    So the second question is that how does initializing with a constant expression/adding the 'final' keyword making it OK, make sense?

  2. #2
    trcooke is offline Tim Cooke
    Join Date
    Jul 2014
    Location
    Belfast
    Posts
    101
    Rep Power
    0

    Default Re: Why can't we have a static (and non final) field in a non-static inner class?

    Can you show an example that demonstrates the scenario you describe? It's much easier to reason about it that way.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Why can't we have a static (and non final) field in a non-static inner class?

    Here's the thing about compile errors: you're not the first one to get them. So you can easily just copy/paste them into google and get a wealth of information about it.

    java - static final field in public nested class - Stack Overflow

    java - Error: field name cannot be declared static - Stack Overflow

    java - Field can't be static? - Stack Overflow

    etc. etc. etc. Just google the literal error you get, "the field can not be declared static in a non-static inner type, unless initialized with a constant expression"
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: Why can't we have a static (and non final) field in a non-static inner class?

    Whenever something is disallowed by the compiler I presume there must be good reason. Examples that are easily demonstrated are accessing instance fields from static contexts, or the put and get problems with extends and super with generics and lists. In this case, I haven't been able to come up with a good example of the problems that might arise other than consistency. Normally, a class may have a static value that is consistent across all instances of the class.

    But in an inner class, the instances are dependent on the instance of the enclosing class. Consider the following example:

    Java Code:
    public class StaticExample {
       public static void main(String[] args) {
          StaticExample foo1 = new StaticExample();
          StaticExample foo2 = new StaticExample();
        
         InnerClass foo11 = foo1.new InnerClass();
         InnerClass foo12 = foo1.new InnerClass();
         
         InnerClass foo21 = foo2.new InnerClass();
         InnerClass foo22 = foo2.new InnerClass();
       }
       public class InnerClass {
          static int value = 0; // not really permitted here
       }
    }
    foo11 and foo12 are instances of the same class. foo21 and foo22 are instances of a different class (because the inner class is tied to instances of the enclosing class). Normally, that would imply that static fields of two different classes would be different. But you can also reference value this way:

    Java Code:
    StaticExample.InnerClass.value;
    But which value? That of foo11 and foo12 or of foo21 and foo22? To resolve this issue, one makes it static and final, then all instances get the same value with no confusion.

    Now I presume the compiler designers could have just decided that all instances of an inner class share the same inner static values. That would make the problem go away. But it would also stray somewhat from other class behavior. In any event I am not fully satisfied with this explanation (I may have made some bad assumptions too). If someone can offer a better explanation, please post it.

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

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

    Default Re: Why can't we have a static (and non final) field in a non-static inner class?

    Quote Originally Posted by jim829 View Post
    In any event I am not fully satisfied with this explanation (I may have made some bad assumptions too). If someone can offer a better explanation, please post it.

    Regards,
    Jim
    It's the assumption I came up with, but then I thought they should have simply declared all static things in a non-static inner class as invalid. I know that (as per your post above) that a static final constant doesn't clearly pose a problem, it also doesn't make much sense (to me). But I'm willing to be persuaded otherwise.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Why can't we have a static (and non final) field in a non-static inner class?

    Think of it: a non static inner class has objects that belong to objects of the outer class; the 'static' things of the inner class objects would be ordinary things of the outer object; that doesn't make sense ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

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

    Default Re: Why can't we have a static (and non final) field in a non-static inner class?

    Quote Originally Posted by JosAH View Post
    Think of it: a non static inner class has objects that belong to objects of the outer class; the 'static' things of the inner class objects would be ordinary things of the outer object; that doesn't make sense ...

    kind regards,

    Jos
    Exactly.
    Which makes the allowance of "static final" attributes in them puzzling.

    And I really should have tried it out...as 'static' is not allowed at all.

    So point (2) in the OP is not a fix.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 15
    Last Post: 06-18-2014, 05:51 PM
  2. Retriving string from private static final class
    By KarlNorway in forum New To Java
    Replies: 9
    Last Post: 12-21-2011, 09:49 PM
  3. Static and non static class methods question
    By silverglade in forum New To Java
    Replies: 2
    Last Post: 05-14-2011, 10:10 PM
  4. How can I share a static field between 2 class loaders?
    By zibilico in forum Advanced Java
    Replies: 6
    Last Post: 03-09-2010, 01:31 PM
  5. static final field
    By techie.it19 in forum New To Java
    Replies: 3
    Last Post: 10-16-2008, 04:12 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
  •