Assertions in static blocks ?

Ulf Zibis Ulf.Zibis at
Tue Mar 9 09:39:36 PST 2010

Am 08.03.2010 23:48, schrieb David Holmes - Sun Microsystems:
> Ulf Zibis said the following on 03/08/10 22:40:
>> Am 07.03.2010 23:20, schrieb David Holmes - Sun Microsystems:
>>> I'm with Keith and Neal, this is all "working as designed". How can 
>>> javac in general know whether or not a class will be initialized.
>> This is not the question here. The question is if it *can* be 
>> initialized, (1) via standard java instruction or (2) via any other 
>> special magic like reflective call from Class.forName(). javac surely 
>> has knowledge about that.
> Both (1) and (2) depend on your whole program not the class being 
> compiled. So javac can not know the answer to this at compile time.

Sorry, maybe I'm missing something. Can you give me an example how 
static initializer could be triggered by (1), if there are only static 
final  primitive type constants in that class?

>> If (1) could never be reached, there should be a warning, in 
>> particular for less experienced programmers, E.g.:
>> "static initializer will never be triggered by any member or method 
>> of this class. Could only be invoked by loading the class explicitly 
>> or inherently(not sure)"
> No that doesn't make sense. A classes own methods/members are not the 
> trigger for classloading and/or initialization. It is the use of those 
> methods/members by other classes that triggers the 
> loading/initialization of the class containing the method/member.

Sorry for the bad wording, I meant "... triggered by access to any 
member or method of this class. ..."
or "There are no members or methods which could trigger the static 
initializer to run, so it may be never reached. ..."

>>> A static initializer is no different to a method or constructor in 
>>> that regard: none of them are ever flagged as unreachable because 
>>> that can't be determined a-priori by javac.
>> IMHO there is a _big_ difference on normal constructors and methods:
>> 1. They _can_ always be executed by normal java instruction.
>> 2. They will _never_ be executed, if not explicitly called from code.
>> On static initializers it's quite different:
>> 1. They _cannot_ be executed by normal java instruction.
>> 2. They will _mostly_ be _automatically_ executed but not ever, if a 
>> member/method from it's class is called
> The implicit vs explicit nature of the execution doesn't change the 
> fact that to determine if either is executed you need to look at the 
> whole program - not just the class(es) being compiled.

Yes, but I can't imagine any other trigger than meant under (2).

Hope, that I'm bothering too much,


More information about the compiler-dev mailing list