aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static Initialization Sequence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static Initialization Sequence" Watch "Static Initialization Sequence" New topic
Author

Static Initialization Sequence

Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125


It compiles and prints out '0'. Isn't it a forward reference at line 2 since p is not complete its initialization yet?
Thanks for your explanation.


SCJP 1.4
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
p starts out with its default value (0). Line 2 is not a "forward reference" because it comes after line 1 in the source code.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125
Originally posted by Ron Newman:
p starts out with its default value (0). Line 2 is not a "forward reference" because it comes after line 1 in the source code.

Thanks Ron. So how about final static variables, will they get a default value also? The following works, but does this mean that the value of final static variable can be changed?
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
It is indeed possible for a supposedly "final" static variable to change its value this way. I modified your code to the following:

which prints out:
0
123
I'd like to see the compiler generate a warning for cases like this, but it doesn't.
[ October 31, 2002: Message edited by: Ron Newman ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static Initialization Sequence