• 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

Ch1 - Member Inner Classes question - in Boyarsky/Selikoff study guide

 
Ranch Hand
Posts: 30
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, a major thank you to Jeanne Boyarsky and Scott Selikoff for monitoring these forums, and specifically to Jeanne who has personally answered several of my "OMG-I-can't-believe-I-didn't-see-that" questions.

Here is another one.  
p25, Member Inner Classes definitions.  The book states that

Member Inner Classes "cannot declare static fields or methods"  (fourth bullet in that section).

However, the following compiles


So...  It seems they can, IF these static fields are marked final.  (Couldn't get a static final method declaration to compile, so I guess this quirk only applies to fields).
I found a couple threads on these boards that discuss this, but none that address what the book states (that I found, at least).   I think it's an important fact one should be aware of when studying for the exam.  Why has the book omitted it?

Thank you in advance!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Elana Abelev wrote:
So...  It seems they can, IF these static fields are marked final.  (Couldn't get a static final method declaration to compile, so I guess this quirk only applies to fields).
I found a couple threads on these boards that discuss this, but none that address what the book states (that I found, at least).   I think it's an important fact one should be aware of when studying for the exam.  Why has the book omitted it?



It is actually more than just final. It has to fulfill all the requirements of a compile time constant.

... and ... when it is a compile time constant, then the field is a compiler constant. Interestingly, the field doesn't actually exist at runtime, as the compiler will, as optimization, insert the value into the generated code directly.

Henry
 
Elana Abelev
Ranch Hand
Posts: 30
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes!  So why does the book say that inner classes cannot declare static fields?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Elana Abelev wrote:Yes!  So why does the book say that inner classes cannot declare static fields?


It can't have static fields. It can have static final fields.

The exact message if you remove final from your example is:
The field B cannot be declared static in a non-static inner type, unless initialized with a constant expression

So if you are asked about static fields, the answer is that they aren't allowed.
 
Elana Abelev
Ranch Hand
Posts: 30
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, but the book doesn't say "unless initialized with a constant expression".   It just gives a blanket "no static fields" statement, which would imply (to many, I'd think) that final fields are included in this definition.  How in the world would one know that final fields are an exception to this rule (without reading the JLS prior to the book)?  It is very misleading.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True, noted.
 
Look! It's Leonardo da Vinci! And he brought a tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic