Monday, May 9, 2011

Run java code without main method

public class test{

 static{

System.out.println("M printing :p");

System.exit(0);

}

}

As soon as class loads it will run static initializer and before it tries to search for main method, we are exiting the program with System.exit, so prints the message and then will exit normally(without throwing any exception)

2 comments:

  1. This really is so good that I had to comment. I'm generally just a lurker, taking in knowledge and nodding my head in quiet approval at the superior stuff?-..this required written props.
    outlook corrupt pst

    ReplyDelete
  2. thanks John for again encouraging to write new posts :)

    ReplyDelete