Static Members in Inner Classes? The Answer
I asked a question a few days ago, regarding static members in inner classes. I presented an example with two static members, one was integer the other Object type, the integer compiled correctly and the Object did not. Here's how it looks like in the IDE:
The answer is found in the Java Language Specification 3rd Edition
, Section 8.1.3
:
Inner classes may not declare static members, unless they are compile-time constant fields
That's the answer. The integer is a compile-time constant which is in-lined by the compiler. The Object is not. Here are some more examples:
By the way, if the class was a static nested class, rather than an inner-class, there wouldn't be any problem defining static members. Plus, constants are inherited when using extensions. This means that the following is legit:
IMHO, Java is not a complex language. It is much simpler than C++, for sure. However, it does have some odd edges.From http://zvikico.typepad.com/problog
He is a entrepreneur and a software architect from Tel Aviv, Israel. He is also a technology freak with about 20 years experience working with computers. He is currently working on his first private initiative in the area of software development tools. His vision is to maximize the abilities of software developers by providing pragmatic tools that enable them to get fast results. Zviki is a DZone MVB and is not an employee of DZone and has posted 25 posts at DZone. You can read more from them at their website.
- Login or register to post comments
- 3097 reads
- Printer-friendly version
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)










Comments
marioschwede replied on Mon, 2008/07/28 - 7:50am