Friday, March 22, 2013

Is possible to print a statement on console without using Main() method in Java?

                        Yes it is possible to write java program without using main() method upto 1.6 version.From 1.7 version onward main() is compulsary.  In java we have the concept static block.static block is executed at the time class loading. So JVM executes static block without object.
                 For this to follow below program
 class Example 
{
   static
    {
       System.out.println("I am executing java program without 
                           using main() method");
     }
}

Output:
            I am executing java program without using main() method
    

No comments:

Post a Comment