File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static initialization" Watch "Static initialization" New topic
Author

Static initialization

adil zahir
Greenhorn

Joined: May 09, 2013
Posts: 29



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 !
Ben Pheonix
Ranch Hand

Joined: Dec 11, 2012
Posts: 46
    
    1

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


Twitter:ben_pheonix
Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 245

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;


Astha - OCPJP 6 (90%)
Ben Pheonix
Ranch Hand

Joined: Dec 11, 2012
Posts: 46
    
    1

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.valueOfI)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.printlnLjava/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
 
 
subject: Static initialization
 
Similar Threads
Static initialization not happening
static final variable initialization inside instance init block
Initializing final arguments
Output or Compilation error? Surprising!
Static