Assertions in static blocks ?

Ulf Zibis Ulf.Zibis at
Sun Mar 7 07:40:43 PST 2010

Am 07.03.2010 02:39, schrieb David Schlosnagle:
> David,
> You're correct, Ulf's example code from earlier today where the actual
> value is initialized in a static initializer block does use getstatic
> and would trigger the EUC_TWMapping3 class's initialization and
> provides the desired functionality.
> Sorry, I meant to clarify the answer to Ulf's original email from
> February 12 "If accessing the static final constants from another
> class, the static block is not executed." showing this isn't a bug. In
> that email's code they were compile time constants, and the JLS
> justifies why the static block wasn't executed. This can lead to
> surprising behavior if you're not aware of it which is one reason I
> prefer extracting any complex initialization behavior into a separate
> method when possible to make it more explicit.

Aside: I prefer short and fast code, and would put the "make it more 
explicit" in a comment. ;-)

In contrast to Keith and Neal, I think, we agree, that this "behaviour 
is surprising", so I would vote at least for a *warning* from javac side.


More information about the compiler-dev mailing list