• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Sybex - OCP 11 Programmer II - Chapter 1 page 12 errata?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The rule for Declaring an Inner Class: Cannot declare static fields or methods, except for static final fields

should this be:  Cannot declare static fields or methods, except for static final fields which are compile time constants?

for example you can declare in an inner class:



but you can't do:



Apologies if this has been mentioned before.
 
Marshal
Posts: 72406
315
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to look in the Java® Language Specification (=JLS):-

JLS §8.1.3 wrote: It is a compile-time error if an inner class declares a member that is explicitly or implicitly static, unless the member is a constant variable (§4.12.4).

...and §4.12.4 tells us,

A constant variable is a final variable of primitive type or type String that is initialized with a constant expression (§15.29).

I think that confirms you are correct. I checked the errata and it would not appear to have been reported before.
The exam will probably use the term from the JLS, “constant variable,” rather than, “compile‑time constant,” even though the English Language Police will issue a $1000 fixed penalty notice to anybody they catch saying, “constant variable”
 
Samuel Burns
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the clarification.  I think more people would read the JLS if it contained more jokes ;)  I'm seriously hoping the exam won't be this pedantic.  In this particular case I couldn't get my head around the 'why' inner classes can't have static members but I've settled for 'because we said so, that's why'.
 
Ranch Foreman
Posts: 323
8
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The OP is correct here.

There are places in Java where each notion of "constant" is relevant, both 'known at compile time' and the less strong "doesn't change in its scope once it is set".

C# has two different keywords for the two concepts, const and readonly, whereas in Java one word, final, (borrowed from C++ const which appears nowhere in Java except reserved-words-never-used) does double duty.
 
Jesse Silverman
Ranch Foreman
Posts: 323
8
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I lied.  It's actually a little worse in Java.

There are three concepts of 'final' or constant in Java.

1. The strongest, a compile-time constant, needed for places like case values in switch statements, among others.  
2. final -- meaning just that it doesn't change again after it is set, but perhaps with a value known only at runtime.
3. Effectively final local variable (which always means that it is NOT marked final), but can be referenced in a lambda or anonymous class.

At least every compile time constant will also be final, tho literals themselves aren't marked as such.

final local variables are not considered 'effectively final'.
The exams are pedantic enough that this matters.
There are places where something needs to be final or effectively final, but despite that final variables are NOT considered "effectively final", a distinction that seems to be splitting hairs but can appear on both mock and real exam questions.  At least they are consistent on this point which seems meaningless to me.
 
Campbell Ritchie
Marshal
Posts: 72406
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe they thought that inner classes are non‑static, so they shouldn't contain static members. Only they still permit constants....
 
Goodbye moon men. Hello tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic