• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static block compilation

 
Abubacker Siddik
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I stumbled across the following code,


My doubt is maybe childish but i am curious. How a static block is compiled and how come the above code is executed perfectly well?

Thanks in Advance,
Abu.A
 
Rob Spoor
Sheriff
Pie
Posts: 20613
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Beginning Java.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49865
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That block is called a static initializer. You will find details in the Java™ Language Specification, but it is by no means easy to read.

Please ask again if you don't understand it.
 
Abubacker Siddik
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My doubt is to know how a static block assigns a variable even before the variable is declared. That's what i intend to ask. Because i thought this would give compile time error. To my wonder it was successfully compiled. Please show me some suggestions.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He did. Static initializers are necessarily run after the class is loaded.
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Visit this link to know how java handles forward reference.

Forward reference
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic