Win a copy of Learning Java by Building Android Games this week in the Android forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

How is a compile-time constant static vaiable causing initialization???  RSS feed

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

According to the JLS 12.4(Initialization of Classes and Interfaces):


A class or interface type will be initialized immediately before the first occurence of any one of the following:
1)T is class and an instance of T is created.
2)T is a class and a static method declared by T is invoked.
3)A static field declared by T is used and the reference to the field is not a compile-time constant. References to compile-time constants must be resolved at compile time to a copy of the compile-time constant value, so uses of such a field never cause initialization.



now see this code:


the o/p is:
the value of j is = 30
20

My question is that how the first line getting printed when niether 1), 2) and 3) point also coming to play???
we r not calling any static method and we are assinging a compile-time constant to variable x......how j is getting initialized??

plz clarify anyone!!!

thanx
amit
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when JVM loads the class , static block will execute.

whenever we interpret any class first that class will be loaded by JVM , after that only main method will execute . thats why first it prints "the value of j is 30" .





S.D.Balasubramani.
SCJP 1.4
 
Balasubramani Dharmalingam
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when JVM loads a class , it will initializes the values for static variables inside that class.
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[amit]: My question is that how the first line getting printed when niether 1), 2) and 3) point also coming to play???

You're calling the main() method. This is a static method of class Y. Thus, point (2) is invoked.
 
Amit Das
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops!!!
okie Jim you r right!!!
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Can anybody explain to me in detail the 3rd point mentioned above? I did not understand that.
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
U mean to say about this point:


3)A static field declared by T is used and the reference to the field is not a compile-time constant. References to compile-time constants must be resolved at compile time to a copy of the compile-time constant value, so uses of such a field never cause initialization


Go thru the url, u will come to know more about it. Do ask any doubts u having.

initializers
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as u said JLS says
T will get initialized when
2)T is a class and a static method declared by T is invoked.

In this case jvm is invoking the main method of the class(which is static method).That is static method of the class is getting invoked, so initialization of the class occurs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!