This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How is a compile-time constant static vaiable causing initialization??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How is a compile-time constant static vaiable causing initialization??? " Watch "How is a compile-time constant static vaiable causing initialization??? " New topic
Author

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

Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
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

Joined: Dec 06, 2004
Posts: 116
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 SD,<br />SCJP 1.4,SCWCD 1.4,SCJP 5.0<br /><a href="http://sd.balasubramani.googlepages.com" target="_blank" rel="nofollow">www.sd.balasubramani.googlepages.com</a>
Balasubramani Dharmalingam
Ranch Hand

Joined: Dec 06, 2004
Posts: 116
when JVM loads a class , it will initializes the values for static variables inside that class.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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.


"I'm not back." - Bill Harding, Twister
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
oops!!!
okie Jim you r right!!!
Ajay Bhargov
Ranch Hand

Joined: Mar 07, 2005
Posts: 31
Hi guys,

Can anybody explain to me in detail the 3rd point mentioned above? I did not understand that.


With Regards,<br />Ajay
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
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

Joined: Feb 26, 2005
Posts: 32
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How is a compile-time constant static vaiable causing initialization???
 
Similar Threads
static initiatization for class and interface
Reg. Interface intialization
Interfaces and call stack; output is not as expected
****Difference in Intialization process of a class and an interface???*****
Static Int and Methods