• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static initialization

 
Greenhorn
Posts: 29
Mac Java ME Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 !
 
Ranch Hand
Posts: 46
1
Tomcat Server Java
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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;
 
Ben Pheonix
Ranch Hand
Posts: 46
1
Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic