Forums Register Login

Static initialization

+Pie Number of slices to send: Send


When I compile the above code, I am getting the output as 100 and static initializer not getting loaded, even though I am calling staticFinalDemo1.var
but :
1 )when i call the method staticFinalDemo1.test() the output is " static initializer 100 "
2) when i change the variable to " static final Integer x =100 " the output is " static initializer 100 "

pleaz some one can explain to me ,i m lost !
(1 cow) 3
+Pie Number of slices to send: Send
Dear Adil,

Thats an interesting question. You can relate to JLS section 12.4 and 12.5 for the class loading and initialization process. Now main issue with your code is,
the class will only be loaded if you are accessing a static variable of the class which is not a constant. In your case, you are declaring the
variable as final, remove the final attribute, and check your class will be loaded and static initializer will run. Below is the modified code.



Quoting directly from JLS.


A class or interface type T will be initialized immediately before the first occurrence
of any one of the following:
• T is a class and an instance of T is created.
• T is a class and a static method declared by T is invoked.
• A static field declared by T is assigned.
• A static field declared by T is used and the field is not a constant variable
(§4.12.4).
• T is a top level class (§7.6), and an assert statement (§14.10) lexically nested
within T (§8.1.3) is executed.



HTH,
Ben
+Pie Number of slices to send: Send
 

Ben Pheonix wrote:Dear Adil,

Thats an interesting question. You can relate to JLS section 12.4 and 12.5 for the class loading and initialization process. Now main issue with your code is,
the class will only be loaded if you are accessing a static variable of the class which is not a constant. In your case, you are declaring the
variable as final, remove the final attribute, and check your class will be loaded and static initializer will run. Below is the modified code.



Quoting directly from JLS.


A class or interface type T will be initialized immediately before the first occurrence
of any one of the following:
• T is a class and an instance of T is created.
• T is a class and a static method declared by T is invoked.
• A static field declared by T is assigned.
• A static field declared by T is used and the field is not a constant variable
(§4.12.4).
• T is a top level class (§7.6), and an assert statement (§14.10) lexically nested
within T (§8.1.3) is executed.



HTH,
Ben



Hello Ben, Great explaination. Please tell why the calss staticFinalDemo1 gets loaded when we declare final Integer in place of int.
static final Integer var= 100;
1
+Pie Number of slices to send: Send
Dear Astha,

The reason is



Its not a compile time constant. A constant can be only of primitive type or String. Integer doesn't fit in it. See JLS 15.28
for definition of compile time constants. Basically in this Integer var is a reference type which is referring to an object of type INT.

I would like to add other stuff as well. Non constant fields are initialized in constructors. Static fields in static constructors and instance fields in instance constructors. For your case,
try making variable of type Integer and view bytecode:


ss staticFinalDemo1 {
static java.lang.Integer var;

static {};
Code:
0: bipush 100
2: invokestatic #5 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
5: putstatic #6 // Field var:Ljava/lang/Integer;
8: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
11: ldc #7 // String Static Initializer
13: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
16: return
}



on lines 0 - 5, value to an var field is assigned. bipush loads 100 to the stack, invokestatic creates Integer object and putstatic saves it to a static variable. In case of int type these lines are absent. Value already exists in constant pool.
HTH,
Ben
today's feeble attempt to support the empire
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 865 times.
Similar Threads
Output or Compilation error? Surprising!
Static
Static initialization not happening
static final variable initialization inside instance init block
Initializing final arguments
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 23:38:50.