Friday, August 27, 2010

Executing Java program without main() function

The title of post seems strange but it is fact. You can develop a Java program without main() function.
Try to execute following program.

class MainMethodNot
{
    static
    {
        System.out.println("This java program have run without the run method");
        System.exit(0);
        
    }
}

The reason that this program executes because static block is executed as soon as the class is loaded. However JVM throws an exception because it doesn't find main() method which can be avoided by using System.exit() function.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.