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
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.
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.
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 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.
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
.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With