Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder DELOCH's Avatar
    Join Date
    Apr 2006
    Location
    Canada
    Posts
    537
    Thanks
    4
    Thanked 2 Times in 2 Posts

    Can somebody explain this error?

    PHP Code:
       Illegal static declaration in inner class ShuPong.ShuPongPanel
           meaning
    modifier 'static' is only allowed in constant variable declarations
           recreate
    :
               public class 
    Dummy {
                   private class 
    Dumbo {
                       private static final 
    int ME 2;
                       private static final 
    String YOU = new String();
                   }
               }
           
    reason: ?
           
    fix: if you make the inner class static, it works
           reason
    : ? 
    It's not an assignment, i just am quite curious why it does that, I can't quite explain it.

    Thanks ahead of time!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    This is because inner classes are associated with instances of their enclosing class. Since static is at a class level and the class is only scoped to the instance of the outer class, it is impossible to create static members in nested classes unless the inner class is converted to a static nested class, or if the member property itself is declared constant with the final modifier.

  • Users who have thanked Fou-Lu for this post:

    DELOCH (08-27-2012)

  • #3
    Regular Coder DELOCH's Avatar
    Join Date
    Apr 2006
    Location
    Canada
    Posts
    537
    Thanks
    4
    Thanked 2 Times in 2 Posts
    So basically ME is associated with the inner class, while YOU is only an object of the inner class, and the actual instance exists outside of the scope of the inner/outer class, so by adding static, i included that scope? Sorry for the lack of better phrasing but that's the best reason I could come up with that kinda made sense to me.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I don't understand that analogy.
    Think of it this way, the inner class if of type new OuterClass().new InnerClass(). So if I do this:
    PHP Code:
    OuterClass.InnerClass obj1 = new OuterClass().new InnerClass();
    OuterClass.InnerClass obj2 = new OuterClass().new InnerClass(); 
    Then obj1 and obj2 do not point to the same class at all (which is a lie, but perceive it as such). The control belongs to the OuterClass, the OuterClass.InnerClass is considered to be scoped only onto the instance of OuterClass, not to the class OuterClass. By doing this, the obj1 and obj2 cannot talk to each other directly to share the data required within a static member. By creating the class as static, you have effectively created a static member for the OuterClass, so that the variables can be shared just like an enum. By keeping it dynamic and writing final to the static property, it now becomes constant so it cannot be changed by any of the classes.
    That make more sense?


  •  

    Posting Permissions

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