Roedy said:
Roedy,
That is a very nice summary, but I have a suggestion
for improvement of that nice table, because
"inline
e.g. int a = 1; [...] Interleaved in textual order with static {} or instance{}."
static init' blocks are done "long" before in-line member
initialization and init blocks, so static {} should NOT be mentioned
in that sentence and
"static init block e.g. static {a = 1;}"
should say: right after the class is loaded by the class loader
The following demostrates the behavior.
public class StaticInitDemo {
static {
System.out.println(
"**Before staticI declaration it can't be referenced " );
}
{
System.out.println( "Before memberI declaration it can't be referenced " );
}
int memberI = 3;
static int staticI = 5;
static {
System.out.println(
"**After in-line staticI declaration: staticI is " + staticI );
}
{
System.out.println( "After memberI declaration: memberI = " + memberI );
}
void incI() {
++memberI;
}
int getI() {
return memberI;
}
public static void main(String[] args) {
System.out.println( "Top of main...");
StaticInitDemo i1 = new StaticInitDemo();
System.out.println( "i1.memberI after creation is " + i1.getI() );
StaticInitDemo i2 = new StaticInitDemo();
System.out.println( "i2.memberI after creation is " + i2.getI() );
i2.incI();
System.out.println( "i2.memberI after incI() is " + i2.getI() );
}
}
results in:
**Before staticI declaration it can't be referenced
**After in-line staticI declaration: staticI is 5
Top of main...
Before memberI declaration it can't be referenced
After memberI declaration: memberI = 3
i1.memberI after creation is 3
Before memberI declaration it can't be referenced
After memberI declaration: memberI = 3
i2.memberI after creation is 3
i2.memberI after incI() is 4
Notice how all static initialization happened before
"Top of main..."
Of course interleaving static and member init' is really ugly form
but it demos the right behavior.
-Paul