Why is main method public, static, and void in Java? Answer (original) (raw)

What is the main method in Java?

The main() method in Java is a special method that is used by JVM to start the execution of any Java program. The main method is also referred to as the entry point of Java application which is true in the case of core Java applications because execution starts from the main() method, 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, also known as Main class, which is later invoked by JVM to start the Java program execution.

For example in the below command :

C:\Documents and Settings\JavaTutorial>edit Calculator.java

code of Calculator class public class Calculator }

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

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

Calculator class is our Main class and it must contain public static void main(String args[]) method to start the program. 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 was 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 in Java 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, synchronized, and 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. This is also different than the main method of C/C++ which returns an integer.

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.

Other Core Java Interview Questions you may like to revise

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