Is the order of static block execution guaranteed ?

R

Razvan

Hi !




Let's suppose that I have the following class:

class CDummy {

static int x;

static { // the first static block
x = 7;
}

// some code: function definitions, attributes etc

static { // the second static block
x = 9;
}
}


The second static block will always execute AFTER the first static
block ? Is this guaranteed, or it can be the other way around ?



What about this:


public class CDummy implements IDummy
{
static { // the only static block
xx = 9;
}

public static void main(String args[])
{
System.out.println("CDummy.");

System.out.println("xx = " + xx);
}

static int xx = 7;
}


The program will print 7 NOT 9. If in the static block the variable
xx is not defined the compiler should complain. OTOH if the variable
xx is defined inside the static block, then the variable xx should be
overwritten with the value 9 but this is not happening. The program
prints 7. Any logical explanation ?


Regards,
Razvan
 
M

Michael Borgwardt

Generally, questions like this can be answered quickly by taking a look
at the language specification available here:
http://java.sun.com/docs/books/jls/second_edition/html/jTOC.doc.html
The second static block will always execute AFTER the first static
block ? Is this guaranteed, or it can be the other way around ?

Section 8.7 of the JLS says

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

public class CDummy implements IDummy
{
static { // the only static block
xx = 9;
}

public static void main(String args[])
{
System.out.println("CDummy.");

System.out.println("xx = " + xx);
}

static int xx = 7;
}


The program will print 7 NOT 9. If in the static block the variable
xx is not defined the compiler should complain. OTOH if the variable
xx is defined inside the static block, then the variable xx should be
overwritten with the value 9 but this is not happening. The program
prints 7. Any logical explanation ?

The declaration and initialization of the variable are separate matters,
even though they're declared in the same line of source code.
 
R

Razvan

public class CDummy implements IDummy
{
static { // the only static block
xx = 9;
}

public static void main(String args[])
{
System.out.println("CDummy.");

System.out.println("xx = " + xx);
}

static int xx = 7;
}


The program will print 7 NOT 9. If in the static block the variable
xx is not defined the compiler should complain. OTOH if the variable
xx is defined inside the static block, then the variable xx should be
overwritten with the value 9 but this is not happening. The program
prints 7. Any logical explanation ?

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,
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.



Regards,
Razvan
 
M

Michael Borgwardt

Razvan said:
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.
 
C

Chris Uppal

Razvan said:
The variable is declared and innitialized AFTER it is assigned,
yet there is no error. If static blocks are executed in 'textual'
order then the static block:

(I'm just saying the same things here as Michael, but using different
wording -- it may help, it may not ;-)

The variable is /declared/ later in the class than the initialise, but that
doesn't matter because it (like all Java variables) is in scope from the
beginning of the class. However you have provided two snippets of code that
"initialise" that variable, one is part of the static { } block, the other is
the real initialiser. The compiler will cause those snippets of code to be
executed in textual order, because that is how Java is defined.

You can disassemble the resulting classfile (with javap, if you have nothing
better to hand) and you'll see that the class initialisation code first assigns
9 to 'xx' and then 7.

It's a bit odd, I know, and I admit that I was surprised too[*], but I think it
does make sense. Anyway, that's the way the language is...

[*] partly because was getting mixed up with the rules for final variable with
constant intialisers -- which are different again.

-- chris
 
R

Razvan

Michael, Chris,




Thanks for your answers. Indeed, what you are saying is logic.




Regards,
Razvan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top