Assertions in static blocks ?

Ulf Zibis Ulf.Zibis at
Sat Mar 6 03:35:03 PST 2010

Am 06.03.2010 02:28, schrieb Keith McGuigan:
> Ulf Zibis wrote:
>> Neal, Keith, thanks for your answer.
>> ... but shouldn't javac claim, that there the code in the static 
>> block will be *never reached*, if there is no trigger to run it?
> Not really.  That would be like claiming that the code in a method 
> will never be reached if no one calls the method.  javac can't know 
> that you're not going to take that class and deploy it somewhere else 
> with other code that will end up calling the method (or initializing 
> the class, etc.).

But javac can know about the risk, because running Class.forName() is 
"not very common", especially if there is nothing but constants in that 
The minimum, javac could do is to throw a warning.

On the other hand, javac throws errors, even if the stated problem never 
             char c1 = 12345;
             char c2 = c1 % 100;
would result in ERROR: "possible loss of precision", but
             c1 *= 12345;
compiles without problem.

Do you know, if following static initializer will be guaranteed to run, 
when accessing the static value? :

class EUC_TWMapping3 extends EUC_TWMapping {
     static final short PL0_5_B2C_RANGE; // plane 0, 5 b2c range
     static final short PLANE_B2C_RANGE; // plane 2..4, 6..15 b2c range
     static {
         PL0_5_B2C_RANGE = 0x2300; // initialized here to force the 
         PLANE_B2C_RANGE = 0x1f00; //      "             "            "
         // assert by values from EUC_TWMapping
         assert PL0_5_B2C_RANGE >= b2c.length;
         assert PLANE_B2C_RANGE <= b2c.length;


More information about the compiler-dev mailing list