Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static blocks

 
Paul Stat
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, this is Q16. from the K&B MasterExam



What is the output?

A: -s
B: -ss
C: -sss
D: -ssss
E: -ssssss
F: Compilation fails

I had it down as C, string builds as follows:

1. s is initially = "-"
2. static block gets called when class loads s="-s"
3. initialisation block then get's called s="-ss"
4. finally go is called from main method s="-sss"

The answer however is B, can someone explain?

[EDIT]
Or is it because static methods have priority over initialisation blocks? So first the static block calls go(), then main() gets it's turn after which the string is -ss and then it prints -ss?

Is that right?
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3. initialisation block then get's called s="-ss"


I dont think the initialisation block at line 7 will be called at all
 
Paul Stat
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himalay Majumdar wrote:
3. initialisation block then get's called s="-ss"


I dont think the initialisation block at line 7 will be called at all


I've edited my message, above is this the reason? In what circumstances WOULD it get called?
 
Sunny Mattas
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I think initialization block gets called only when object is created.In your code no object is being created ,so no call to initialization block.
This may be because one uses initialization block to initialize the values of object.

Regards
Sunny Mattas
SCJP5
 
Paul Stat
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sunny Mattas wrote:Hi
I think initialization block gets called only when object is created.In your code no object is being created ,so no call to initialization block.
This may be because one uses initialization block to initialize the values of object.

Regards
Sunny Mattas
SCJP5


Aha! You're correct thankyou, the following



Produces

static
initialisation
main
-sss
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In what circumstances WOULD it get called


Static block is run everytime the class is loaded.
Instance initialization block helps only after a new instance is created.



 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dint see its already done.
 
teja dharma
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the above program first the initial value of s="-" then static block is called so the value of s is now "-s" and then the control comes to main method which again calls go method so now s is"-ss" thats all !
1)"-" /during initialization/
2)"-s" /when the static block is called/
3)"-ss" /when go method is called from main method ./

I think now you got cleared your doubt
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We could do without the big fonts and the fancy colors...
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HLI HAI
 
sudipto shekhar
Ranch Hand
Posts: 823
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himalay Majumdar wrote: HLI HAI


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic