What is Public static void main

In most of the Programming language, main function is the first function where program start execution. This is the first method from where Java Virtual Machine(JVM) start executing. Following are the variation of main method.

What is Public static void main

In most of the Programming language, main function is the first function where program start execution. This is the first method from where Java Virtual Machine(JVM) start executing. Following are the variation of main method.

What is Public static void main

What is Public static void main

In most of the Programming language, main function is the first function where program start execution. This is the first method from where Java Virtual Machine(JVM) start executing. Following are the variation of main method.

public static void main(String[] args) 
public static void main(String... args)
public static void main(String args[]) //this is most classic signature of main method.

Remember the varargs version of java will work in java 1.5 or later version only.

Main is the entry point of  the class. In java everything is written  within the class so, when you run the java program on the command prompt , loader will load the class and jvm will search for the main method . so making main as static will allow jvm to access without creating object. Main method will strictly follow the rules otherwise your program will not run.

Signature of main method in java:

Public:  main method is declared public because it is visible to other object of other type. It must be public otherwise it not be possible to call it.

Static: main method is static because jvm will call it without creating object. If main is not declared as static then jvm should create a instance of main Class, constructor can be overloaded and can have arguments there would not be any certain way for JVM to find main method in Java.

Void : main is declared void, because main method  does not suppose to return any value, void mean not returning anything.

Example: A simple program how to write a program in java using main method.

class Helloworld
 {
  public static void main(String args[])
     {
       System.out.println("Hello world"); 
      }
  }
  

Output: After compiling and executing the program output will be as follows.

Download Source code