It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes intiliazation blocks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "intiliazation blocks" Watch "intiliazation blocks" New topic

intiliazation blocks

Chetan Dodiya
Ranch Hand

Joined: Jun 27, 2008
Posts: 39
class Tarsier{
static String s="-";
public static void main(String [] args){
static void go(){s+="s";}

the answer is "-ss"
but why "-sss"
why the {go();} is not executeg???

SCJP 1.5
M Srilatha
Ranch Hand

Joined: Aug 27, 2008
Posts: 137
Please quote the source from where you got the question!

{go();} is not executed because its a instance initialization block.
In the main() method, no object of the class is created. So this block doesnt execute. Add Tarsier t = new Tarsier(); to the main() method. Then it will execute!

Thanks,<br />Srilatha M
I agree. Here's the link:
subject: intiliazation blocks
It's not a secret anymore!