File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static block compilation

 
Abubacker Siddik
Ranch Hand
Posts: 90
  • 0
  • 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: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Beginning Java.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Visit this link to know how java handles forward reference.

Forward reference
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic