Wednesday, July 28, 2010

Can we write a java program with out a main() method?

Yes. You can write in a static block what ever you want to execute.
for ex: public class myclass{
static {
S.o.p("my class without main() method ");
System.exit(0);

}
}
now you can execute the above class and it will print the above output.
note that the line System.exit(0) is necessary , otherwise it will throws an exception that no main() method is found

No comments:

Post a Comment