This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach 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 Murach's Java Servlets and JSP this week in the Servlets 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: 19649
    
  18

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: 37865
    
  22
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static block compilation
 
Similar Threads
coding doubts of static methods
String buffer
static block
static block
enum question