• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static Initializer Blocks

 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Praveen Babu
Ranch Hand
Posts: 138
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic