• 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

Doubt in Java Puzzlers by Joshua Bloch and Neal Gafter

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,
I could not understand the program execution of the Java Puzzlers of puzzle 49:


when you execute this program it will print Elvis wears a size -1930 belt why?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The explanation provided by the book is very detail (and very enlightening, if I may add). What is it about the explanation that is still confusing to you? We can't elaborate on the answer, if you don't tell us what you don't understand about the answer.

Henry
 
Raj chiru
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,
Thanks For Your Reply,
Actually where I'm confusing is,we are explicitly declared the static field values,right?But why First,static fields are set to default values?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static initializers are executed in declared order. So in this case, INSTANCE is initialized first, then CURRENT_YEAR. However, Elvis needs CURRENT_YEAR but it hasn't been initialized yet. Because the JVM simply does not initialize CURRENT_YEAR right there and then its default value is used.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

raj chiru wrote:
Actually where I'm confusing is,we are explicitly declared the static field values,right?But why First,static fields are set to default values?



Well, which is worse? A default value or an undefined random value?

From this puzzle, you can see that initialization is not atomic, and it is possible to see variables before they get assigned. So, yes. Static fields are set to default values -- because the values is not known yet. But that is better than an undefined random value, which is what it would be seen as if the variable isn't set.

BTW, there is an exception to this rule. If the static variable is a compile time constant, then the value is guarranteed to be known before it is used -- and it will be set correctly to the compile time constant value.

Henry
reply
    Bookmark Topic Watch Topic
  • New Topic