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


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

Static blocks

Paul Stat
Ranch Hand

Joined: Jan 20, 2009
Posts: 50
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

Joined: Sep 28, 2008
Posts: 324
3. initialisation block then get's called s="-ss"


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


SCJP 1.6, SCWCD 5.0, SCBCD 5.0 [loading..]
Paul Stat
Ranch Hand

Joined: Jan 20, 2009
Posts: 50
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

Joined: Apr 22, 2008
Posts: 45
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


Regards
Sunny Mattas
SCJP5
Paul Stat
Ranch Hand

Joined: Jan 20, 2009
Posts: 50
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

Joined: Sep 28, 2008
Posts: 324
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

Joined: Sep 28, 2008
Posts: 324
I dint see its already done.
teja dharma
Ranch Hand

Joined: Feb 07, 2009
Posts: 51
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


SCJP 5
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
We could do without the big fonts and the fancy colors...
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
HLI HAI
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 823

Himalay Majumdar wrote: HLI HAI




Regards, Sud.
SCJP 5 ScjpFAQ JLS
 
Consider Paul's rocket mass heater.
 
subject: Static blocks