• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why must non-static inner classes declare static fields final?

 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I came across this question:

Can someone explain why the static fields have to be final for the NonStaticInner class?
is that the only limitation for non-static inner classes?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the JLS, §8.1.2 Inner Classes and Enclosing Instances:


An inner class is a nested class that is not explicitly or implicitly declared static. Inner classes may not declare static initializers (�8.7) or member interfaces. Inner classes may not declare static members, unless they are compile-time constant fields (�15.28).


You can glean two things from that. First, all inner classes are non-static. A nested static class is called a nested class, not an inner class. Secondly, it's a rule of the Java language that inner classes can not have static members.
I hope that helps,
Corey
 
Brian Joseph
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you once again, Corey. Up until your post I was not making the distiction between nested and inner classes.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why cannot inner classes have static fields?


[ June 03, 2003: Message edited by: Marlene Miller ]
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marlene
I get an error with your code. I am using JDK 1.4.
Me and my complier think that inner classes can not have static declarations . My complier says
Error:
Outer.java:5: inner classes cannot have static declarations
static long nextId = 0;
^
1 error
[ June 03, 2003: Message edited by: Anupam Sinha ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anumpam. Yes, you should get a compiler error. static members are not allowed to be declared in inner classes. That is the rule.
I would like to know *why* the language does not allow static fields that are not compile-time constants to be declared in inner classes.
My examples were meant to show what I think are reasonable things one might want to do.
[ June 03, 2003: Message edited by: Marlene Miller ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Marlene Miller:
I would like to know *why* the language does not allow static members to be declared in inner classes.


I know I've seen better discussions on this before but, being that I'm at a loss to find any, I'll simply direct you to this thread.
I believe the short of the matter was that disallowing them would simplify the language somewhat but I've never really found an answer for this question that I could really be happy with.
Sorry I can't be of more help,
Corey
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Corey, I once posted a more general question. Kathy replied. Jose and Dan were going to reply but deferred to Kathy. I revised my question to the more specific one above. No one responded. Here
I have seen pages and pages of discussion about this question on the Sun Java Advanced Features forum. No one knows why.
[ June 03, 2003: Message edited by: Marlene Miller ]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I understand well the following excerpt from Inner Classes Specification Kathy was deadly right is her response.


The static declaration modifier was designed to give programmers a way to define class methods and class variables which pertain to a class as a whole, rather than any particular instance. They are "top-level" entities.
The static keyword may also modify the declaration of a class C within the body of a top-level class T. Its effect is to declare that C is also a top-level class. Just as a class method of T has no current instance of T in its body, C also has no current instance of T. Thus, this new usage of static is not arbitrary.
As opposed to top-level classes (whether nested or not), inner classes cannot declare any static members at all. To create a class variable for an inner class, the programmer must place the desired variable in an enclosing class.
It is helpful at this point to abuse the terminology somewhat, and say, loosely, that the static keyword always marks a "top-level" construct (variable, method, or class), which is never subject to an enclosing instance.
This shows why an inner class cannot declare a static member, because the entire body of the inner class is in the scope of one or more enclosing instances.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic