Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in what order are static blocks and static variables in a class executed? [duplicate]

Tags:

java

static

final

Possible Duplicate:
Java static class initialization

Why is the string variable updated in the initialization block and not the integer(even though the block is written first)

class NewClass
{
    static 
    {
       System.out.println(NewClass.string+" "+NewClass.integer);
    }

    final static String string="static";
    final static Integer integer=1;

    public static void main(String [] args)//throws Exception
    {
    }
}

My output is

static null

P.S:Also noticed that string variable initialization happens before the block only when i insert the final modifier. why is that?why not for integer as well?I have declared it as final static too

like image 487
tr_quest Avatar asked Sep 16 '12 16:09

tr_quest


People also ask

Which will execute first static block or static variable?

Answer is to use Static block as they get initialized before main so you can use them to print anything without having any dependency on main Method in java.

Which is executed first static block or instance block?

Order of execution When you have all the three in one class, the static blocks are executed first, followed by constructors and then the instance methods.

Are static variables initialized first?

It is a variable which belongs to the class and not to object(instance ). Static variables are initialized only once, at the start of the execution. These variables will be initialized first, before the initialization of any instance variables.

How many copies of static and class variables are created when?

How many copies of static and class variables are created when 10 objects are created of a class? Explanation: Only one copy of static variables are created when a class is loaded. Each object instantiated has its own copy of instance variables. 9.


2 Answers

From section 12.4.2 of the JLS, snipped appropriately:

The procedure for initializing C is then as follows:

  • Then, initialize the final class variables and fields of interfaces whose values are compile-time constant expressions (§8.3.2.1, §9.3.1, §13.4.9, §15.28).

  • Next, execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in textual order, as though they were a single block.

So for non-compile-time-constants, it's not a case of "all variables" and then "all static initializers" or vice versa - it's all of them together, in textual order. So if you had:

static int x = method("x");

static {
    System.out.println("init 1");
}

static int y = method("y");

static {
    System.out.println("init 2");
}

static int method(String name) {
    System.out.println(name);
    return 0;
}

Then the output would be:

x
init 1
y
init 2

Even making x or y final wouldn't affect this here, as they still wouldn't be compile-time constants.

P.S:Also noticed that string variable initialization happens before the block only when i insert the final modifier.

At that point, it's a compile-time constant, and any uses of it basically inlined. Additionally, the variable value is assigned before the rest of the initializers, as above.

Section 15.28 of the JLS defines compile-time constants - it includes all primitive values and String, but not the wrapper types such as Integer.

like image 147
Jon Skeet Avatar answered Oct 03 '22 04:10

Jon Skeet


Here is a short and straight forward answer to you question....

static Variable :

static Variables are executed when the JVM loads the Class, and the Class gets loaded when either its been instantiated or its static method is being called.

static Block or static Initializer Block :

static static Initializer Block gets Initialized before the Class gets instantiated or before its static method is called, and Even before its static variable is used.

///////// Edited Part /////////

class NewClass {

    final static String string = "static";
    final static Integer integer = 1;

    static {
        System.out.println(NewClas.string + " " + NewClas.integer);
    }

    public static void main(String [] args) { // throws Exception
        new NewClas();
    }

}

The above will print static 1.

The reason is that the JVM will do the optimization process known as Constant folding, doing an pre-calculation of the constant variables.

Moreover in your case the result was static null cause Constant folding is applied to Primitive type and not Wrapper Object, in your case its Integer...

like image 24
Kumar Vivek Mitra Avatar answered Oct 03 '22 04:10

Kumar Vivek Mitra