aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static Initializer Blocks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static Initializer Blocks" Watch "Static Initializer Blocks" New topic
Author

Static Initializer Blocks

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9243
    
    1

Anyone please provide me with explanation of the following code. I tried it and I'm failing to understand. The code as below,



Can anyone help me with this. I want to know what is the use of sf1=10 in the static block. Is there anyway where in I can print 10 instead of 30 with the above code?

Regards,
Jothi Shankar Kumar. S


SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
Praveen Babu
Ranch Hand

Joined: Jul 30, 2006
Posts: 138
See JLS here,

http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.3.2.3
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9243
    
    1

Hi Praveen,

I appreciate your help but unfortunately, I have been there before and that was not clear for me. So anyone please explain it.

Regards,
Jothi Shankar Kumar. S
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
All static variables and static initializer blocks are run first as in the same order as written in the source java program when the first time loading class.

The question just tests for this point. The following code is one way print out 10 in this order by call another method:

See examples in Initialization
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static Initializer Blocks
 
Similar Threads
StatFanta
initialization
Static Initializer Block
Static initaializer block & forward referencing -Question from SCJP 6 book by Khalid Mughal
How to use this() in overloaded subclass constructors ( and why) ?