wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why must non-static inner classes declare static fields final? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why must non-static inner classes declare static fields final?" Watch "Why must non-static inner classes declare static fields final?" New topic
Author

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

Brian Joseph
Ranch Hand

Joined: May 16, 2003
Posts: 160
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?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
Brian Joseph
Ranch Hand

Joined: May 16, 2003
Posts: 160
Thank you once again, Corey. Up until your post I was not making the distiction between nested and inner classes.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Why cannot inner classes have static fields?


[ June 03, 2003: Message edited by: Marlene Miller ]
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
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

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Dec 20, 2001
Posts: 3271
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

Joined: Mar 05, 2003
Posts: 1391
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 ]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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.


SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why must non-static inner classes declare static fields final?