• 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 ...
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Sybex - OCP JSE 11 - 1Z0-819 - Chapter 8 - P 318

Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Jeanne, Scott, Staff team,

In the "Why the Hippo Program Printed C After AB" table

The penultimate sentence says:

"For the exam, you just need to know that a class must be initialized before it is referenced or used."

There is one catch to this rule, using constants in a class.

A constant is a static final variable that either of type primitive or String.

If the constant is defined in the same line as it is declared, the static initializer-blocks are no more called.

It is a corner case, thus, not really useful in real life, just thought I would share it.

For example,


Saloon Keeper
Posts: 1313
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am going to predict that staff here will check the JLS.
If it is specified behavior, they will confirm it as a corner case and assent.
If the behavior in this regard is not specified in the JLS, they will consider it an implementation artifact, even if all popular JDK/JVM variants seem to agree.

Section 8.7 didn't answer the question:

But in section 12.4.1 it looks like while a corner case, this is official required behavior as per the JLS (oops, I linked to version 15 but doubt it changed since 11).
I'd say you were possibly pedantic, but definitely correct.

• A static field declared by T is used and the field is not a constant variable

I have learned my lesson about inferring rules from runtime behavior that might be a non-required implementation artifact.

That section 12.4 gives way more detail about static initializer rules and regulations than I remembered from Enthuware, Sybex or DurgaSoft material, which were three sources that go into a fair amount of tutorial on that topic.

Did you notice the suggested detail from behavior or direct JLS reference or tutorial material alluding to the rule?
This is my favorite show. And this is my favorite tiny ad:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic