Java Blocks–Advanced–Part 1

Following a previous post I will deep into the discussion about anonymous blocks mixed with static variables.

Lets see a simple example, and then we will complicate a little bit.

public class TestStaticInitializer {

{
System.out.println(“block 1”);
}

public TestStaticInitializer() {
System.out.println(“static x = ” + x);
}

private static int staticInitialization() {

System.out.println(“static initialization of x”);

return 5;
}

{
System.out.println(“block 2”);
}

static int x = staticInitialization();

public static void main(String[] args) {

System.out.println(“static main block before calling the constructor”);

TestStaticInitializer test = new TestStaticInitializer();

System.out.println(“static main block after constructor”);
}

{
System.out.println(“block 3”);
}

}

You can observe that:
– There is a static variable ‘x’ that its initialized calling a static method.
– There are 3 anonymous blocks.
– The constructor calls the static variable.

The main method references the class.

At first sight we can think the anonymous blocks are called in sequence, which it’s true. Although if there is a static variable initialized in anyway, this initialization runs before any anonymous block.(Why? Because a class is loaded in 2 phases: load class, run class. At load the JVM after some verification, initialized every static variable and runs any static anonymous initializer, and only after runs the class, or our instance.)

You can observe this by looking to the execution output:

static initialization of x
static main block before calling the constructor
block 1
block 2
block 3
static x = 5
static main block after constructor

The anonynous blocks are executed by sequence, but the initialization variable “static initialization of x” runs first, although its declaration is after “block 2”.

The sequence is:

1 – Initialize the static variable x

2 – run main method

3 – initialize the class (because I’m calling the constructor)

4 – run anonymous blocks

5 – runs the constructor (only after the static blocks)

6 – runs the other “main” statements.

You must be wondering why I underlined “The main method references the class” above.

Lets see if I remove (I will just comment it) the call to the class constructor.

public class TestStaticInitializer {

{
System.out.println(“static block 1”);
}

public TestStaticInitializer() {
System.out.println(“static x = ” + x);
}

private static int staticInitialization() {

System.out.println(“static initialization of x”);

return 5;
}

{
System.out.println(“static block 2”);
}

static int x = staticInitialization();

public static void main(String[] args) {

System.out.println(“static main block before calling the constructor”);

//TestStaticInitializer test = new TestStaticInitializer();

System.out.println(“static main block after constructor”);
}

{
System.out.println(“static block 3”);
}

}

and the output is:

static initialization of x
static main block before calling the constructor
static main block after constructor

Well, where are the anonymous blocks initializers? Thet are not needed! We haven’t crated any instance of the class, so the JVM just considered the static stuff!

And if a static block references the static variable x?

public class TestStaticInitializer {

{
System.out.println(“block 1”);
x = 7;
System.out.println(“block 1 after x = 7”);
    }

public TestStaticInitializer() {
System.out.println(“static x = ” + x);
}

private static int staticInitialization() {

System.out.println(“static initialization of x”);

return 5;
}

{
System.out.println(“block 2”);
}

static int x = staticInitialization();

public static void main(String[] args) {

System.out.println(“static main block before calling the constructor”);

//TestStaticInitializer test = new TestStaticInitializer();

System.out.println(“static main block after constructor”);
System.out.println(“x = ” + x);
    }

{
System.out.println(“block 3”);
}

}

The output:

static initialization of x
static main block before calling the constructor
static main block after constructor
x = 5

The initialization of “x = 7” in the first anonymous block is simple ignored, because the code was never ran!

static int x;// = staticInitialization();

The output will show you “x = 0”.

I hope this post will help you understand the mix between anonymous and static initialization blocks. In a future post I will bring some class hierarchy to this discussion.

2 Comments

  1. Andriy says:

    As I know “static block 1” isn’t static. Because it is concatenated to begin of default constructor.
    To make this block static we shoul add static word. For example static { … }

  2. josedacruz says:

    You are right!
    I have done a great explanation about the mix between static variables and methods and non static anonymous initializers, but the title was wrong. So I changed some explanations and adapted to the “new reality”.
    Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *

Bad Behavior has blocked 159 access attempts in the last 7 days.

Hyper Smash