Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static block variable Vs static variable

 
kri shan
Ranch Hand
Posts: 1470
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static block loads before constructor gets executed. What about static variables outside static block(within the class) ?
 
Miklos Szeles
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kri,

Have you tried it? I found this:

According to one of my favorite authors, David Flanagan, author of Java in a Nutshell,

"What the compiler actually does is to internally produce a single class initialization routine that combines all the static variable initializers and all of the static initializer blocks of code, in the order that they appear in the class declaration. This single initialization procedure is run automatically, one time only, when the class is first loaded."


 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The official version is in the Java™ Language Specification, but it is by no means easy to read.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic