Static Members in Inner Classes? The Answer
Join the DZone community and get the full member experience.
Join For Freei 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:
Opinions expressed by DZone contributors are their own.
Comments