This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Static block compilation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static block compilation" Watch "Static block compilation" New topic
Author

Static block compilation

Abubacker Siddik
Ranch Hand

Joined: Aug 12, 2009
Posts: 90
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

Joined: Oct 27, 2005
Posts: 19685
    
  20

Moving to Beginning Java.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
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

Joined: Aug 12, 2009
Posts: 90
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

Joined: Sep 29, 2008
Posts: 12617

He did. Static initializers are necessarily run after the class is loaded.
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
Visit this link to know how java handles forward reference.

Forward reference


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Static block compilation