Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Static blocks

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dint see its already done.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HLI HAI
 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Himalay Majumdar wrote: HLI HAI



 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic