• 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

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

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when JVM loads a class , it will initializes the values for static variables inside that class.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops!!!
okie Jim you r right!!!
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
A wop bop a lu bop a womp bam boom! Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic