Why is main method public, static, and void in Java? Answer

What is the main method in Java?
the main method in Java is a standard method that is used by JVM to start the execution of any Java program. the main method is referred to as the entry point of Java application which is true in the case of core java applications but in the case of container-managed environments like Servlet, EJB, or MIDlet this is not true as these Java programs have their own life-cycle methods like init(), service() or destroy() for Servlet's which is used by the container. The main method in Java is run by the main thread which is a non-daemon thread and the Java program runs until the main method finishes or any other user thread is running. 

When we start JVM by running the java command we also provide the name of the class that contains the main method, which is later invoked by JVM to start Java program execution. for example in the below command :
C:\Documents and Settings\JavaTutorial>edit Calculator.java

code of Calculator class
public class Calculator{
   public static void main(String args[]){
        System.out.println("I am main class which 
         contains the main method");
   }
}

C:\DOCUME~1\JavaTutorial>javac Calculator.java

C:\DOCUME~1\JavaTutorial>java Calculator
I am main class which contains the main method

Calculator class must contain public static void main(String args[]) method. Now if we change the signature of the main method and try to run the same Java program we will get an error as shown below :

public class Calculator{
   public static void main(String args){
        System.out.println("I am main class which contains 
                  the main method");
   }
}

C:\DOCUME~1\sharma>javac Calculator.java

C:\DOCUME~1\sharma>java Calculator
Exception in thread "main" java.lang.NoSuchMethodError: main

because we have changed String[] to String which means JVM not able to find a standard main method that is required to start execution of the Java program. You can learn more about the main method by joining these free Java Programming courses online. 


Valid Signature of the main method in Java 

the main method is a standard method and has a pre-specified signature if you change the signature of the main method JVM will not be able to locate the main method will throw Exception at runtime as shown in the above example.

The main method is public, static, and void and accepts a String[] as an argument and from Java 5 onwards it can also accept variable arguments instead of arrays. following signatures are valid main method signatures in Java :

public static void main(String args[]) {}

public static void main(String[] args){}

public static void main(String... args){}

You can also use certain modifiers like final, synchronizedand strictfp along with the main method in Java.




Why the main method is public static and void in Java

This is a very famous core Java interview question and appeared on many fresher and experience level interviews. Though every java programmer uses the main method not everyone is familiar with the reason why the main is static or why the main is public in Java. 

Here are few reasons which make sense to me to think of why main is public, static, and void in java

1. The main method in Java is public so that it's visible to every other class, even which are not part of its package. if it's not public JVM classes might not able to access it.

2. The main method is static in Java so that it can be called without creating any instance. While JVM tries to execute the Java programs it doesn't know how to create instances of the main class as there is no standard constructor is defined for the main class.

3. The main method is void in Java because it doesn't return anything to the caller which is JVM.



Difference between C, C++, and Java main method

If you come from C and C++ programming language then you know what is the main method as both of these programs also use main() as an entry point for program execution but the main method in C and C++ is quite different than the main method in Java. 

The main method in java doesn't return anything and has return type void while the main method is C and ++ return int.


That's all on what is the main method in Java, What are the valid signature of the main method in Java, What happens if you put the incorrect signature of the main method, Why the main method is public, static, and void in Java and finally we saw the difference between C, C++ and Java main methods. 

This is one of the most important fundamentals of the Java programming language and every Java programmer should be familiar with it.

16 comments:

  1. Main has been declared as Public as it can be accessed globally. And static so that It can be accessed without creating the instance of the class in which Main method has been defined. As Jvm starts execution from main method in java.

    ReplyDelete
  2. that main metoh concept is not clear

    ReplyDelete
    Replies
    1. Hello @Unknown, what is your doubt? Which aspect of main() method is not clear? Can you please let me know, I'll try to make it clear.

      Delete
  3. This is very precise explanation. I wish i read that before the interview i gave yesterday. :(

    ReplyDelete
    Replies
    1. Thank you @Emtiaz, glad that you find these questions and explanations useful.

      Delete
  4. Got other things but can you explain the reason why was the need to make the main method parametric with a single dimension String array. I know we can use it as a CLA and save space of an extra variable, but why it was so necessary to put it there?

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

    ReplyDelete
    Replies
    1. Hello @Unknown, can you post your code, it may be possible that your main method has wrong syntax and that's why Java compiler is complaining.

      Delete
  6. not able to understand anything

    ReplyDelete
    Replies
    1. Hello @Anonymous, that's sad, how can I help you? If you have a specific doubt we may be able to explain you better.

      Delete
  7. Hi Javin, just wanted to thank you for responding to questions from 2016 to 2020 and, I hope, even till now.

    Good explanation for myself in my opinion.

    For those who still are not clear on the main method part:

    Maybe I am oversimplifying things in my head, however the way I see it everything needs to start somewhere and it just so happens for Java that is defined to be at main with the argument specifically being String[]

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.