File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Initializing a static variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Initializing a static variable" Watch "Initializing a static variable" New topic

Initializing a static variable

Bob Beerbower
Ranch Hand

Joined: Jun 06, 2001
Posts: 32
I'm going through the HFJ chapter that deals with staic variables and I have a question about initialization. HFJ gives the following code.

I understand this code and the use of the static initalizer, but I'm confused as to the next block of code.

HFJ states that this second code example will generate a compiler error that

I confused becuase on the previous page they stated that
"Defualt values for declared but uninitialized static and instance variables are the same:
primitive integers (long, short, etc.): 0
primitive floating point (float, double):0.0

So in the case of the second block of code, why is the compiler complaining, should'nt BAR_SIGN be initialized to the default (0.0)?

edited 5/15 - error correction
[ May 15, 2005: Message edited by: Bob Beerbower ]

Regards Bob Beerbower
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
No, since it's final, you have to specifically initialize it to a value.

James Carman, President<br />Carman Consulting, Inc.
Bob Beerbower
Ranch Hand

Joined: Jun 06, 2001
Posts: 32
Thanks for the reply. If I'd kept on reading HFJ gave me the same answer a few pages latter. Thanks again.
I agree. Here's the link:
subject: Initializing a static variable
It's not a secret anymore!