Tuesday, 7 July 2015

Tagged Under: ,

Java Main Method and Why it's declared as public static void main(String args[])

Share
Main method in Java is used by JVM to start execution of any Java program. This method must appear within a class. Main method is entry point of Core.

 Java application.But In case of server related applications like EJB,Servlet these Java programs have there own life-cycle methods like init(), service() or destory().

Java Main Method:

The main() method in C++, C# and Java are static because they can then be invoked by the runtime engine without having to instantiate an instance of the parent class.

Syntax: public static void main(String[] args) 


Java Main Method and Why it's declared as public static void main(String args[])

Java Main Method Example :

Source Code:
 
class TechnicalUpdatesHub {

  public static void main(String args[])
   { 

      System.out.println("Hey I am in Technical Updates Hub main Method");

   }

   public static  void main(int i)
   { 
      System.out.println("Hey I am in Technical Updates Hub Overloaded main Method");  
   } 
 
 }

Output:

Hey I am in Technical Updates Hub main Method
Hey I am in Technical Updates Hub Overloaded main Method


Java main method is static because JVM can call it without creating any instance of class. main method is public in Java because JVM can easily access and execute it.

Main method in Java is not supposed to return any value. Note: JVM throws NoSuchMethodException:main. if it doesn't find main method of predefined signature in class.

Main method is run by a special thread called "main" thread in Java. Main method in Java can be overloaded like any other method in Java but JVM will only call main method with specified signature.

0 comments:

Post a Comment

Contact Us

Want to ask anything? Be our guest, give us a message.

Name Email * Message *

FaceBook Like