File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


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

intiliazation blocks

Chetan Dodiya
Ranch Hand

Joined: Jun 27, 2008
Posts: 39
class Tarsier{
static String s="-";
public static void main(String [] args){
go();
System.out.println(s);
}
{go();}
static{go();}
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: intiliazation blocks