File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 19541
    
  16

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: 36478
    
  16
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
enum question
static block
static block
String buffer