Re: Is the order of static block execution guaranteed ?

From: Michael Borgwardt (brazil_at_brazils-animeland.de)
Date: 09/09/04


Date: Thu, 09 Sep 2004 10:39:34 +0200

Razvan wrote:
>>The declaration and initialization of the variable are separate matters,
>>even though they're declared in the same line of source code.
>
>
> The variable is declared and innitialized AFTER it is assigned,

No. The declaration and initialization are after the assignment in the
source code, but they don't *happen* in that order.

Please realize that the source code is *not* executed, the compiled byte
code is.

> yet there is no error. If static blocks are executed in 'textual'
> order then the static block:
>
> static { // the only static block
> xx = 9;
> }
>
>
> should complain that xx is not declared.

No. Why do you think so?

When dealing with specifications, you need to be hair-slittingly exact in
your terminology. The JLS does NOT say

"static blocks are executed in 'textual' order"

It says:

"static initializers and class variable initializers are executed in textual order"

In your example, there is a static initializer and after it
a class variable declaration combined with a class variable initializer.

The declaration is never "executed", it just causes the compiler to insert a
field in the resulting class file. Then, during runtime, the two initilizers
are executed in the (textual) order in which the compiler encountered them
in the source code.

it is true that at the time the compiler encounters the static initializer,
it doesn't yet "know" the corresponding variable declaration, but that is
alo true for any use of the variable in a method prior to the declaration.
The JLS does not forbid this, and the compiler has to accomodate for this by
building an internal representation of the entire class (which it probably
needs to do anyway) and check consistency only when this is complete.



Relevant Pages

  • Re: anonymous initializer problem
    ... the declaration section (via initializer). ... and has only one public constructor that takes an array of field objects. ... This way I could define my dictionary in the declaration section where it is ... costructor declare custom static arrays that use the dictionary values, ...
    (microsoft.public.dotnet.languages.csharp)
  • PROBLEM: Compile Break 2.6.0-test2: More Broken Stuff
    ... declaration of `DECLARE_TASK_QUEUE' ... initializer but incomplete type ... drivers/net/wan/sdlamain.c:224: unknown field `routine' specified in ... send the line "unsubscribe linux-kernel" in ...
    (Linux-Kernel)
  • Re: Compiling Guppy-PE extension modules
    ... >> The MSDN help gives a simple example of code raising compiler error ... src\sets\sets.c: error C2099: initializer is not a constant ... I don't know if it applies here, but in this context the extern keyword ...
    (comp.lang.python)
  • Re: Arrays of incomplete type
    ... Even if `struct opaque' ... this declaration would be incorrect as ... Your comment said it would be incorrect ... with an initializer equal to 0." ...
    (comp.std.c)
  • Re: ping source code
    ... >I was reading through original source code of ping for some insight ... and types (defaulting to int if not specified) given between the ... the older type of declaration; I'm not sure about C99 - implicit int is ...
    (comp.lang.c)