Meaning of public static void main in java / Understanding main method in java / Everything about main in java

Main method : Overview

Anyone who is familiar with java must be knowing main method. Whenever a class is executed using java command(or through an IDE), it is this method which is called first. In other words, main method is the entry point of a class or an application(if there are multiple classes). This post focuses on the detailed explanation of signature of this method.

Main method : Signature

Signature of main method is public static void main(String[] args). It consists of 4 keywords where public is the access specifier, static is an access modifier, void is the return type and main is the name of method. String array argument of this method is used for holding parameters sent to the class. Any command line parameters supplied while executing a class are automatically populated in this array.
All of these keywords of main method are explained in detail below.

public : This keyword makes main method exposed to outside this class. Since this method is always called from outside a class, it should be declared as public else it will not be visible external to the class. If main method is preceded with any other access specifier(private or protected), then it would not be visible to JVM and an error will be thrown. Example,

public class MainNotPublic {
 
     // main method has protected access
     protected static void main(String[] args) {
	System.out.print("Main is protected");
     }
 
}

Above class declares main method as protected and gets the following message when executed.

Error: Main method not found in class MainNotPublic , please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

static : Applying static to a field or method binds it to the class. This means that in order to access a static field or method, instance of the class is not required and it can be directly accessed using class name.  Also, there is only one copy of a static field or method and it is shared by all instances of the class.
When a class is executed, there is no instance of it. Hence, in order to call a method without an instance, it should be marked as static. Thus main method needs to be static.

What should happen when main is not declared static as in the class below

public class MainNotStatic {
 
     // main method is non-static
     public void main(String[] args) {
	System.out.print("Main is not static");
     }
 
}

You are right, an error is received when this class is executed.

Error: Main method is not static in class MainNotStatic, please define the main method as:
public static void main(String[] args)

void : Return type of a method indicating that the method does not return any value. In java, main method does not return any value, hence it should be preceded with void.
If the main method is given any other return type, then an Error will be thrown at execution time. Example, suppose a class has following main method.

public class MainWithReturn {
 
     // main method returning an integer  
     public static int main(String[] args) {
		return 0;
     }
 
}

This class compiles fine but when executed shows the following.

Error: Main method must return a value of type void in class MainWithReturn, please
define the main method as:
   public static void main(String[] args)

All the above errors will only be shown when a class is executed from command line using java command. When executing from an IDE such as eclipse, you will not see an option to run the class as a Java program.

Can I create a method with name main in a class ?

Off course you can create a method which has name as main but it will be an overloaded version of main method and will be treated as just like any other method. Look at the below example,

public class SecureImp{
   public static void main(String[] args) {
	System.out.println("Entry method of class");
        // create an object of this class
	SecureImp obj = new SecureImp();
        // call method 
        obj.main(""); 
   }
 /**
   * Another method with name as main 
   **/
   public void main(String args) {
	System.out.println("Another main method");
   }
}

Above class contains two method with same name but with different argument types. When the class is executed, main method runs which creates an object of this class and calls another method which also has name as main. Output is :

Entry method of class
Another main method

Note that if argument count and types of both methods will be same, then the class will not compile.

Let’s tweak in

  1. String[] args can also be written as String args[].
  2. It is not necessary to name the argument array to args. It can be any name as per java variable naming rules.
  3. public and static are interchangeable. This means that main method can also be declared as static public void main(String[] a).
  4. Any parameters passed to a class are automatically populated in the String array parameter of the main method.