File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Amit Das
Ranch Hand
Posts: 206
  • 0
  • 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
 
Balasubramani Dharmalingam
Ranch Hand
Posts: 116
  • 0
  • 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
  • 0
  • 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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops!!!
okie Jim you r right!!!
 
Ajay Bhargov
Ranch Hand
Posts: 31
  • 0
  • 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.
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • 0
  • 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
 
p anish
Ranch Hand
Posts: 32
  • 0
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic