• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Elana Abelev
Ranch Hand
Posts: 30
4
  • Mark post as helpful
  • send pies
  • 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!
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Yes!  So why does the book say that inner classes cannot declare static fields?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
551
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Posts: 37507
551
Eclipse IDE Java VI Editor
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!